feat: implement request locking for SeleniumBase

This commit is contained in:
Rodrigo Verdiani 2025-11-08 20:36:18 -03:00
parent a87e00bc80
commit 30cae1a384
2 changed files with 44 additions and 35 deletions

44
app.py Normal file
View File

@ -0,0 +1,44 @@
import json
import time
import threading
from flask import Flask, request
from seleniumbase import SB
rest_port = 8090
app = Flask(__name__)
# Global lock to ensure only one SeleniumBase instance at a time
sb_lock = threading.Lock()
@app.route("/url", methods=['POST'])
def process_url():
data = request.get_json()
if not data or 'url' not in data:
return json.dumps({"error": "URL parameter is required"}), 400
url = data['url']
# Try to acquire the lock
with sb_lock:
# Only one request at a time can enter this block
try:
with SB(uc=True, pls="none") as sb:
sb.activate_cdp_mode(url)
sb.sleep(5)
sb.uc_gui_click_captcha()
time.sleep(10)
data = {
"page_source": sb.get_page_source()
}
return json.dumps(data), 200, {'Content-Type': 'application/json'}
except Exception as e:
return json.dumps({"error": str(e)}), 500
if __name__ == "__main__":
# Allow Flask to handle concurrent requests, but they'll queue on the lock
app.run(host='0.0.0.0', port=rest_port, threaded=True)

View File

@ -1,35 +0,0 @@
import json
from flask import Flask, request
from seleniumbase import SB
import time
rest_port = 8090
app = Flask(__name__)
@app.route("/url", methods=['POST'])
def hello():
data = request.get_json()
if not data or 'url' not in data:
return json.dumps({"error": "URL parameter is required"}), 400
url = data['url']
with SB(uc=True) as sb:
sb.driver.page_load_strategy="eager"
sb.maximize_window()
sb.activate_cdp_mode(url)
sb.sleep(5)
sb.uc_gui_click_captcha()
time.sleep(10)
data = {
"pageSource": sb.get_page_source()
}
return json.dumps(data), 200, {'Content-Type': 'application/json'}
if __name__ == "__main__":
app.run(host='0.0.0.0', port=rest_port)