V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
NeverBelieveMe
V2EX  ›  Python

django 使用 celery 的时候,报错 You cannot call this from an async context - use a thread or sync_to_async.

  •  
  •   NeverBelieveMe · 12 天前 · 466 次点击

    代码示例

    def func_curd(data):
        obj = DemoModel.objects.filter(id=data["id"]).first()
        # obj = DemoModel.objects.get(id=data["id"])
    
    
    @celery_app.task(bind=True)
    def demo_task(self,data):
        func_curd(data)
    

    报错内容

    raised unexpected: SynchronousOnlyOperation('You cannot call this from an async context - use a thread or sync_to_async.')
    
    Traceback (most recent call last):
      File "/app/apps/ai_management/task/coze_task.py", line 244, in query_coze_workflow_async_with_callback
        insert_data_into_db(page_task_id, [])
      File "/app/apps/ai_management/task/coze_task.py", line 39, in insert_data_into_db
        page_task = ReviewPageTask.objects.filter(id=page_task_id).first()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 1057, in first
        for obj in queryset[:1]:
                   ~~~~~~~~^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 398, in __iter__
        self._fetch_all()
      File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 1881, in _fetch_all
        self._result_cache = list(self._iterable_class(self))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 91, in __iter__
        results = compiler.execute_sql(
                  ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1560, in execute_sql
        cursor = self.connection.cursor()
                 ^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 330, in cursor
        return self._cursor()
               ^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 306, in _cursor
        self.ensure_connection()
      File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
        self.connect()
      File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 271, in connect
        self.set_autocommit(self.settings_dict["AUTOCOMMIT"])
      File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 483, in set_autocommit
        self.ensure_connection()
      File "/usr/local/lib/python3.12/site-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.
    

    我之前的项目里面,都没出现过 celery 任务中查询数据库会报错的问题。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2748 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:00 · PVG 18:00 · LAX 02:00 · JFK 05:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.