fix(websockets): Initialise io_loop in multi-worker environments #16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Addressed
This PR addresses the issue where WebSocket connections frequently fail when running a Django application with Bokeh integration using multiple Gunicorn workers. In multi-worker environments, there was only a 1/n chance (where n is the number of workers) that the WebSocket connection would succeed.
Solution
The solution implements a more robust approach to io_loop initialisation by:
IOLoop.current()
if it doesn't existThis change removes the worker affinity requirement, allowing any worker to successfully handle WebSocket connections regardless of which worker handled the initial HTTP request.
Changes Made
WSConsumer.application_context
to initialise theio_loop
rather than raising an errorawait
to some un-awaited coroutinesTesting
Tested with Gunicorn running with 4 Uvicorn workers, confirming:
Closes #15