Skip to main content
 首页 » 编程设计

python之celery 倒计时错误

2025年05月04日9lexus

我正在尝试为任务添加倒计时,但没有成功。

这是我使用的代码

@celery.task 
def check_st(key, t): 
    device = Device.query.filter(Device.key == key).first() 
    data = Data.query.filter(Data.dev_id == device.id).order_by(Data.timestamp.desc()).first() 
    if(data.timestamp == t): 
        return True 

我是这样调用它的

check_st.apply_async([key, data.timestamp], countdown=300) 

取自完整 celery log .

有什么想法吗?

编辑

用倒计时测试一个简单的例子似乎工作正常。我想是我的项目布局和我运行 celery 的方式导致了这个问题。

所以我的布局是这样的

proj(app(__init__.py, views.py, models.py, tasks.py ))

在我的 tasks.py 中,我导入了这样的模块

from app.models import Device, Data 

然后在我的 views.py 中导入任务使用

from app.tasks import check_st 

然后我像这样在目录 proj/ 中运行 celery

celery -A app.tasks worker -l info 

使用此设置,所有其他任务都按预期工作,但是当我添加倒计时关键字时,我得到以下信息

Task app.tasks.check_st[deffd607-c6d4-41af-882c-e111736888ba] raised exception: TypeError("check_st() got an unexpected keyword argument 'countdown'",) 

请您参考如下方法:

问题终于解决了。解决方案来自于改变

celery -A app.tasks worker -l info 

到

celery --workdir=proj/ -A app.tasks worker -l info