如何创建一个包装器,使芹菜任务看起来像asyncio.Task
?还是有更好的方法把芹菜和asyncio
结合起来?
@asksol,芹菜的创造者,said this::
It's quite common to use Celery as a distributed layer on top of async I/O frameworks (top tip: routing CPU-bound tasks to a prefork worker means they will not block your event loop).
但是我找不到任何专门针对asyncio
框架的代码示例。
您可以使用
run_in_executor
将任何阻塞调用包装到任务中,如documentation中所述,我还在示例中添加了一个自定义timeout:如官方网站所述,芹菜5.0版将有可能做到这一点:
http://docs.celeryproject.org/en/4.0/whatsnew-4.0.html#preface
以上内容引用自上一链接。
所以最好的办法就是等待5.0版的发布!
同时,快乐的编码:)
我发现最干净的方法是将} )中:
async
函数包装在asgiref.sync.async_to_sync
(来自^{我从我写的blog post中提取了这个例子。
相关问题 更多 >
编程相关推荐