-->

Making Tornado websocket handler thread safe

2019-07-15 23:26发布

问题:

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.

回答1:

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