Chaining websockets from an url to server to other services #2081
-
Hi, I am new to flask_socketio. I am attempting to stream live stock data from finnhub websocket url to a client and potentially more services. The reason I'm chaining this is because finnhub websocket only allows 1 connection at a time. Initially the code does not work until I put it socketio.sleep(0), which enable concurrency ? I really wanted to know why this suddenly work because I was stuck with this for 3 days. Here is the code def on_message(ws, message):
print(message)
socketio.sleep(0)
#why does adding a sleep here make it works ?
socketio.emit('live_stock', message)
def live_stock_stream():
global ws_app
websocket.enableTrace(True)
ws_app = websocket.WebSocketApp(
"wss://ws.finnhub.io?token=mytoken",
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open
)
ws_app.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
@socketio.on('connect')
def connect():
global ws_thread
with thread_lock:
if ws_thread is None:
ws_thread = socketio.start_background_task(live_stock_stream) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Are you using eventlet or gevent for concurrency? If yes, I suggest you uninstall them and use the regular threads, which would not need you to sleep to achieve concurrency. |
Beta Was this translation helpful? Give feedback.
Are you using eventlet or gevent for concurrency? If yes, I suggest you uninstall them and use the regular threads, which would not need you to sleep to achieve concurrency.