Skip to content
Advertisement

Serving file asyncronously with Django and uvicorn

I have a Django view that serves the content of a file. The Django application was running with WSGI until recently. This worked fine. Then I adapted my application to use ASGI running uvicorn. The file serving is now broken as it seems to loose the connection.

How can I serve the file asynchronously with Django and uvicorn?

Current view:

JavaScript

The server is throwing the following error:

JavaScript

I’m using:

JavaScript

Advertisement

Answer

I can’t reproduce with your example, but this question seems to relate to your problem: channels#1722.

Check the dr-luk response:

As @fmgoncalves has mentioned, one way is to alter how the files are served, but I have found a little more reliable patch that I have implemented in my Django Channels Setup based on the information provided by their post.

It seems that the change of thread_sensitive=False to default to True is causing these deadlock detection messages. As far as I can see, Django Channels doesn’t seem to mind the occurrences of the deadlocks.

That being said, I felt it would be safe to monkey patch the sync_to_async function for my Django installation.

project_app/moneky_patches.py

JavaScript

Then you just overwrite the existing instance of asgiref’s sync_to_async method with our patched wrapper that enforces thread_sensitive=False

project_app/__init__.py

JavaScript

This is make channels, and all of Django run in a insensitive manner like it did before the ASGIRef update.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement