I am creating a web application which has a websocket handler. On each successful connection i am appending the websocket handler object to a list. Another handler class called PostResultHandler accepts POST data. This PostResultHandler will be called by a background process which sends json data. Once this json data is received by PostResultHandler, I want write to the list of websockets.
Currently I am just iterating through the list of websockets and writing the json data to it. But i think it may be a blocking call and the background process which calls the PostResultHandler will be blocked until the result is written to all websockets.
Is there any way to make this piece of code non blocking so that the background process will keep running without any delay
The Tornado examples folder includes a chat demo with websockets, it simply does:
for waiter in cls.waiters:
try:
waiter.write_message(chat)
except:
logging.error("Error sending message", exc_info=True)
This is not a blocking call. The message is buffered on the server immediately and your code continues executing.