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:
class FileServeView(View):
def get(self, request, *args, **kwargs):
# ...
return HttpResponse(
FileWrapper(file_content), content_type="application/octet-stream"
)
The server is throwing the following error:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 377, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/asgi2.py", line 17, in __call__
await instance(receive, send)
File "/usr/local/lib/python3.8/site-packages/channels/http.py", line 192, in __call__
await self.handle(body_stream)
File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 382, in __call__
raise RuntimeError(
RuntimeError: Single thread executor already being used, would deadlock
I'm using:
Django 3.2.13
uvicorn 0.17.1
channels 2.3.1
asgiref 3.5.0
I can't reproduce with your example, but this question seems to relate to your problem: channels#1722.
Check the dr-luk response: