我们可以在异步 python 函数中调用 celery delay 或 apply_async

Can we call celery delay or apply_async within an async python function

假设Redis 是我们的Celery 消息队列。 apply_async 是否被认为是阻塞 I/O 函数,换句话说,这个代码在 django 3.1 视图中是否正确,或者它会阻塞事件循环并需要 sync_to_async 包装:

async def django_view(request):
       celery_task.apply_async()
       return success_page

我已经看到使用 FastAPI 时,如果 redis 数据库关闭,所有应用程序都会挂起等待连接。 对于“所有应用程序”,我的意思是其他请求也被卡住了。 这意味着发出调用的协程没有挂起:它正在等待同步调用并阻止所有其他调用。

这是我如何解决的示例:

self.loop = asyncio.get_event_loop() 
partial_delay = lambda: entry.celery_task.delay(
    command_type=entry.command_type, command_body=entry.command_body
        )
self.loop.run_in_executor(None, partial_delay)