首页 > 解决方案 > 在异步函数(通道消费者)Django中从数据库中获取模型

问题描述

概括

您好,目前我正在开发一个通过使用 django-channels 将数据实时发送到前端的应用程序。我已经能够对打开页面时创建的所有模型执行此操作,但是当页面尚未打开时,我还不能获取以前的模型。

代码

下面是我第一次尝试让这个系统工作,当消费者连接方法被触发时,这将被调用,在连接被接受后,我将运行这个 for 循环

for app in Application.objects.all():
    app_json = ApplicationSerializer(app).data
    await self.send_json({'action': 'create', 'data': app_json})

运行此代码时,我会收到错误消息 django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

这是有道理的,因为消费者的连接方法是异步的,所以我按照消息的建议继续使用sync_to_async

for app in sync_to_async(Application.objects.all)():
    app_json = ApplicationSerializer(app).data
    await self.send_json({'action': 'create', 'data': app_json})

我决定sync_to_async在 Application 对象周围使用,因为错误消息突出显示了 for 循环行本身,而且我知道 applicationSerializer 会正常工作,因为我在其他异步方法中使用它就好了

这导致TypeError: 'coroutine' object is not iterable

最后的想法

我对 django-channels 和异步都是新手,如果我知道通道将大量基于异步,我会在开始这个项目之前研究系统。

我查看了其他堆栈溢出帖子,发现可能映射数据可能是一个解决方案,但我不能 100% 确定这是异步问题还是 django-channels

我还可以尝试哪些其他事情?

更新

根据 Kens 的建议,我继续尝试以下代码:

   apps = await database_sync_to_async(Application.objects.all)()
   for app in apps:
       app_json = ApplicationSerializer(app).data
       print(app_json)

这返回了堆栈跟踪

Exception inside application: You cannot call this from an async context - use a thread or sync_to_async.
Traceback (most recent call last):
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\sessions.py", line 254, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\auth.py", line 181, in __call__
    return await super().__call__(scope, receive, send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\middleware.py", line 26, in __call__
    return await self.inner(scope, receive, send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\routing.py", line 150, in __call__
    return await application(
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\consumer.py", line 58, in __call__
    await await_many_dispatch(
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\utils.py", line 51, in await_many_dispatch
    await dispatch(result)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\consumer.py", line 73, in dispatch
    await handler(message)
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\generic\websocket.py", line 196, in websocket_receive
    await self.receive(text_data=message["text"])
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\channels\generic\websocket.py", line 259, in receive
    await self.receive_json(await self.decode_json(text_data), **kwargs)
  File "D:\HomeAutomation\HomeAutomation\consumers.py", line 28, in receive_json
    for app in apps:
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\django\db\models\query.py", line 287, in __iter__
    self._fetch_all()
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-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 "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\lib\site-packages\django\db\models\sql\compiler.py", line 1154, in execute_sql
    cursor = self.connection.cursor()
  File "C:\Users\Devin\.virtualenvs\HomeAutomation-l9uhKhE0\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.

标签: pythondjangoasynchronousdjango-channels

解决方案


这段代码sync_to_async(Application.objects.all)()创建了一个需要等待而不是直接使用的协程。对于您的具体情况,最好先在 for 循环之外获取数据,然后继续。此外,您可能想database_sync_to_async改用。

from channels.db import database_sync_to_async

apps = await database_sync_to_async(Application.objects.all)()
for app in apps:
    app_json = ApplicationSerializer(app).data
    await self.send_json({'action': 'create', 'data': app_json})

附带说明一下,确保在序列化程序序列化应用程序实例时不会有数据库查询。为此,请使用select_relatedprefetch_related预取可能需要的任何嵌套对象

更新:是的,由于某些 Django ORM 方法(如 )的惰性.all(),数据库实际上不会在调用方法时被命中,而是在使用时被命中。强制它立即执行的一种常见方法是将查询集转换为列表。试试这个:

from channels.db import database_sync_to_async

apps = await database_sync_to_async(list)(Application.objects.all())
for app in apps:
    app_json = ApplicationSerializer(app).data
    await self.send_json({'action': 'create', 'data': app_json})

推荐阅读