Python Django从Django通道异步websocket使用者引发SynchronousOnlyOperation异常

Python Django从Django通道异步websocket使用者引发SynchronousOnlyOperation异常,python,django,python-asyncio,django-celery,django-channels,Python,Django,Python Asyncio,Django Celery,Django Channels,目前,我有一个异步使用者,如下所示: class AsyncDashConsumer(AsyncJsonWebsocketConsumer): async def connect(self): await self.accept() async def disconnect(self, code): await self.close() async def receive(self, text_data=None, bytes_dat

目前,我有一个异步使用者,如下所示:

class AsyncDashConsumer(AsyncJsonWebsocketConsumer):

    async def connect(self):
        await self.accept()

    async def disconnect(self, code):
        await self.close()

    async def receive(self, text_data=None, bytes_data=None, **kwargs):
        print('>>>>>>Data received from client:', text_data)
        # get data
        # sendData.send(self.channel_name)
        sync_to_async(sendData.send(self.channel_name))
        print('>>>>>>>First Worker Start')
        sync_to_async(sendData2.send(self.channel_name))
        print('>>>>>>>Workers started')

    async def Dash_tester(self, event):
        await self.send(text_data=event['text'])

    async def Dash_tester2(self, event):
        await self.send(text_data=event['text'])
async def receive
中的
sendData
函数是戏剧性的任务:


我的问题是
同步到异步(sendData.send(self.channel\u name))
部分。当我尝试使用所有这些时,客户端浏览器成功地获取了从戏剧任务发送的文本字符串,但出现了以下错误:

>>>>>>Data received from client: WEBSOCKET OPEN
Unexpected failure in after_enqueue.
Traceback (most recent call last):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\dramatiq\broker.py", line 98, in emit_after
    getattr(middleware, "after_" + signal)(self, *args, **kwargs)
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\middleware.py", line 21, in after_enqueue
    Task.tasks.create_or_update_from_message(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\models.py", line 16, in create_or_update_from_message
    task, _ = self.using(DATABASE_LABEL).update_or_create(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\models\query.py", line 587, in update_or_create
    with transaction.atomic(using=self.db):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\transaction.py", line 175, in __enter__
    if not connection.get_autocommit():
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\backends\base\base.py", line 389, in get_autocommit
    self.ensure_connection()
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\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.
>>>>>>>First Worker Start
Unexpected failure in after_enqueue.
Traceback (most recent call last):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\dramatiq\broker.py", line 98, in emit_after
    getattr(middleware, "after_" + signal)(self, *args, **kwargs)
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\middleware.py", line 21, in after_enqueue
    Task.tasks.create_or_update_from_message(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\models.py", line 16, in create_or_update_from_message
    task, _ = self.using(DATABASE_LABEL).update_or_create(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\models\query.py", line 587, in update_or_create
    with transaction.atomic(using=self.db):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\transaction.py", line 175, in __enter__
    if not connection.get_autocommit():
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\backends\base\base.py", line 389, in get_autocommit
    self.ensure_connection()
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\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.
>>>>>>>Workers started
我认为将
sync\u to_async
包装器添加到
sendData.send
函数可以删除此错误,但它没有。我已经尝试了在讨论这个问题的其他线程上提供的解决方案,但是没有一个能够阻止错误的发生。我对python asyncio有一个非常基本的了解,如果我遗漏了一些简单的东西,那么很抱歉

>>>>>>Data received from client: WEBSOCKET OPEN
Unexpected failure in after_enqueue.
Traceback (most recent call last):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\dramatiq\broker.py", line 98, in emit_after
    getattr(middleware, "after_" + signal)(self, *args, **kwargs)
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\middleware.py", line 21, in after_enqueue
    Task.tasks.create_or_update_from_message(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\models.py", line 16, in create_or_update_from_message
    task, _ = self.using(DATABASE_LABEL).update_or_create(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\models\query.py", line 587, in update_or_create
    with transaction.atomic(using=self.db):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\transaction.py", line 175, in __enter__
    if not connection.get_autocommit():
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\backends\base\base.py", line 389, in get_autocommit
    self.ensure_connection()
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\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.
>>>>>>>First Worker Start
Unexpected failure in after_enqueue.
Traceback (most recent call last):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\dramatiq\broker.py", line 98, in emit_after
    getattr(middleware, "after_" + signal)(self, *args, **kwargs)
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\middleware.py", line 21, in after_enqueue
    Task.tasks.create_or_update_from_message(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django_dramatiq\models.py", line 16, in create_or_update_from_message
    task, _ = self.using(DATABASE_LABEL).update_or_create(
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\models\query.py", line 587, in update_or_create
    with transaction.atomic(using=self.db):
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\transaction.py", line 175, in __enter__
    if not connection.get_autocommit():
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\site-packages\django\db\backends\base\base.py", line 389, in get_autocommit
    self.ensure_connection()
  File "C:\Users\Timothee Legros\PycharmProjects\QuadkoRepository\Venv\lib\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.
>>>>>>>Workers started