如何在芹菜任务中使用异步功能?
我找到了一些类似的问题,但找不到我想要的。
我具有异步功能,我想在芹菜任务中使用它,但不能将其称为内部任务。有什么办法吗?
db.py
async def select_users():
sql = "SELECT * FROM Users WHERE "
sql, parameters = self.format_args(sql, parameters=kwargs)
return await self.execute(sql, *parameters, fetchrow=True)
tasks.py
from .celery import app
import db
@app.task
def update_credits():
users = db.select_users() #here I should call func with 'await'
print(users)
I've found some similar questions but couldn't find what I want.
I have async function which is I want to use it inside my celery task but cannot call it with await inside task. Is there any way to do it?
db.py
async def select_users():
sql = "SELECT * FROM Users WHERE "
sql, parameters = self.format_args(sql, parameters=kwargs)
return await self.execute(sql, *parameters, fetchrow=True)
tasks.py
from .celery import app
import db
@app.task
def update_credits():
users = db.select_users() #here I should call func with 'await'
print(users)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行以下操作:
希望这对您有所帮助。
You can do something like the following:
Hope this help you.
您可以使用事件循环方法 :
但是,如果您不
等待
db.select_users()
coroutineasync def
您的代码不是通过使用asyncio
获得任何改进。You can use the Event Loop method
run_until_complete
:But if you do not
await
thedb.select_users()
coroutine inside anasync def
your code is not getting any improve by usingasyncio
.