Hi,
I am attempting to upgrade from Django 3.0 to
3.1 and asgiref 2.x to 3.x, and noticed a breakage in a view which I suspect might be related
to its usage of StreamingHttpResponse. The code in question now raises
the following exception:
2020-12-08 17:50:05,159 [ERROR] daphne.server: Exception inside application: You cannot call this from an async context - use a thread or sync_to_async.
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/channels/staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "/usr/local/lib/python3.8/dist-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/asgi.py", line 168, in __call__
await self.send_response(response, send)
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/asgi.py", line 242, in send_response
for part in response:
File "/main/srhaque/Innovatieltd/source/paiyroll/utils/time_machine.py", line 202, in fill
for files in filler:
File "/main/srhaque/Innovatieltd/source/paiyroll/views/employee/mobile_views.py", line 636, in regenerate_documents
pay_run_db = models.PayRun.objects.get(id=pay_run['id'])
File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 425, in get
num = len(clone)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 269, in __len__
self._fetch_all()
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.8/dist-packages/django_viewflow-1.6.1-py3.8.egg/viewflow/managers.py", line 79, in __iter__
for process in base_iterator:
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1154, in execute_sql
cursor = self.connection.cursor()
File "/usr/local/lib/python3.8/dist-packages/django/utils/asyncio.py", line 24, in inner
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
(There is a subsequent exception, appended below, which I assume that is caused by this exception). I see that the immediate cause of the problem is the ORM query in my mobile_views.py, but I cannot seem to find the correct way to wrap the query in sync_to_async() or database_sync_to_async(). Any hints or pointers would be most welcome.
Attachment: second exception for reference...
2020-12-08 17:50:05,168 [ERROR] asyncio: Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /usr/local/lib/python3.8/dist-packages/twisted/internet/asyncioreactor.py:287
handle: <TimerHandle when=115234.625186167 AsyncioSelectorReactor.callLater.<locals>.run() at /usr/local/lib/python3.8/dist-packages/twisted/internet/asyncioreactor.py:287>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.8/dist-packages/twisted/internet/asyncioreactor.py", line 290, in run
f(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/daphne/server.py", line 295, in application_checker
protocol.handle_exception(exception)
File "/usr/local/lib/python3.8/dist-packages/daphne/http_protocol.py", line 301, in handle_exception
self.basic_error(500, b"Internal Server Error", "Exception inside application.")
File "/usr/local/lib/python3.8/dist-packages/daphne/http_protocol.py", line 342, in basic_error
self.handle_reply(
File "/usr/local/lib/python3.8/dist-packages/daphne/http_protocol.py", line 242, in handle_reply
raise ValueError("HTTP response has already been started")
ValueError: HTTP response has already been started