mher / flower

Real-time monitor and web admin for Celery distributed task queue
https://flower.readthedocs.io
Other
6.46k stars 1.09k forks source link

django.core.exceptions.SynchronousOnlyOperation on calling EP /flower/api/task/async-apply/<task_name> #1214

Open minoriadeunosolo opened 2 years ago

minoriadeunosolo commented 2 years ago

Describe the bug When calling the EP /flower/api/task/async-apply/ (no matter the name of the task) an django.core.exceptions.SynchronousOnlyOperation exception is raised and returns a HTML 500 error, and the task is successfully enqueued in celery.

NOTE: DJANGO_ALLOW_ASYNC_UNSAFE is NOT set.

Detailed tracelog: HTTPServerRequest(protocol='http', host='.azurewebsites.net', method='POST', uri='/flower/api/task/async-apply/', version='HTTP/1.0', remote_ip='127.0.0.1') Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/tornado/web.py", line 1702, in _execute result = method(*self.path_args, self.path_kwargs) File "/usr/local/lib/python3.8/site-packages/tornado/web.py", line 3173, in wrapper return method(self, *args, *kwargs) File "/usr/local/lib/python3.8/site-packages/flower/api/tasks.py", line 221, in post response.update(state=result.state) File "/usr/local/lib/python3.8/site-packages/celery/result.py", line 478, in state return self._get_task_meta()['status'] File "/usr/local/lib/python3.8/site-packages/celery/result.py", line 417, in _get_task_meta return self._maybe_set_cache(self.backend.get_task_meta(self.id)) File "/usr/local/lib/python3.8/site-packages/celery/backends/base.py", line 609, in get_task_meta meta = self._get_task_meta_for(task_id) File "/usr/local/lib/python3.8/site-packages/django_celery_results/backends/database.py", line 118, in _get_task_meta_for obj = self.TaskModel._default_manager.get_task(task_id) File "/usr/local/lib/python3.8/site-packages/django_celery_results/managers.py", line 111, in get_task return self.get(task_id=task_id) File "/usr/local/lib/python3.8/site-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/site-packages/django/db/models/query.py", line 431, in get num = len(clone) File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 262, in len self._fetch_all() File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 51, in iter results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1173, in execute_sql cursor = self.connection.cursor() File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 31, in inner raise SynchronousOnlyOperation(message) django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

To Reproduce Steps to reproduce the behavior:

  1. call to /flower/api/task/async-apply/
  2. See error HTML 500 and django.core.exceptions.SynchronousOnlyOperation on logs

Expected behavior HTML 200 and { "state": "PENDING", "task-id": "abc300c7-2922-4069-97b6-a635cc2ac47c" }

System information python 3.8 Django==3.2.12 django-celery-results==2.3.1 flower==1.0.0

settings.py CELERY_RESULT_BACKEND = 'django-db' CELERY_BROKER_URL = redis://xxxxxxx:root@redis-cache:6379'

flower's docker image (mher/flower) without changes

ljluestc commented 1 year ago

from asgiref.sync import sync_to_async from django.core.cache import cache

def execute_task_sync(task_name, *args, *kwargs): async_execute_task = sync_to_async(execute_task) # Replace with your actual task execution function return async_execute_task(task_name, args, **kwargs)