Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Tornado websocket handler thread safe

I am randomly getting error 1006 ( (I failed the WebSocket connection by dropping the TCP connection) when trying to write messages from threads using Tornado's websocket server handler.

I created N threads and passed my ws_handler to them. But when I start using

self.ws_handler.write_message(jsondata)

for a large number of threads, I keep getting the same error.

From what I understand, 1006 is TCP connection dropped when a 'heartbeat' communication is skipped between websockets. I am guessing this is because of threads running in parallel and trying to send messages. I tested it using 2-3 threads and it works fine but for large number it doesn't.

I wonder if there's any method to achieve message sending within threads.( meaning lock being handled internally by ws_handler and sending accordingly).

One solution I am thinking of is to push jsondata into a queue and have another single thread push the messages, but I fear that would create a bottleneck.

My client is AutobahnPython.

like image 295
crazydiv Avatar asked Jul 29 '26 06:07

crazydiv


1 Answers

Tornado is based on a single-threaded event loop; all interactions with Tornado objects must be on the event loop's thread. Use IOLoop.current().add_callback() from another thread when you need to transfer control back to the event loop.

See also http://www.tornadoweb.org/en/stable/web.html#thread-safety-notes

like image 121
Ben Darnell Avatar answered Jul 31 '26 21:07

Ben Darnell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!