gpt4 book ai didi

python - 发送 2 个参数到 Celery eta 任务

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:14 24 4
gpt4 key购买 nike

我使用 eta 来编写任务:

test.apply_async(eta=datetime(2019, 8, 4, 17, 01))

但是我在 test.py 上有一个任务,它从 View 中获取参数,例如“post”:

app = Celery()
@app.task(bind=True)
def test(post, self):
#somecode

我需要传递“eta”和“post”,我尝试:

test.apply_async(post, eta=datetime(2019, 8, 4, 17, 01))

但是报错:

functools.partial object argument after * must be an iterable, not Author

出了什么问题?

编辑1

尝试:

test.apply_async(args=(post.id,) eta=datetime(2019, 8, 4, 17, 01))

它尝试运行任务:

@app.task(bind=True)
def test(post, self):
takeit = Author.objects.get(pk=post)
print takeit

结果:

Traceback (most recent call last):
File "c:\python27\lib\site-packages\celery\app\trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "c:\python27\lib\site-packages\celery\app\trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
File "pathto/test.py", line 122, in test
takeit = Author.objects.get(pk=post)
File "c:\python27\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "c:\python27\lib\site-packages\django\db\models\query.py", line 371, in get
clone = self.filter(*args, **kwargs)
File "c:\python27\lib\site-packages\django\db\models\query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "c:\python27\lib\site-packages\django\db\models\query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "c:\python27\lib\site-packages\django\db\models\sql\query.py", line 1250, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "c:\python27\lib\site-packages\django\db\models\sql\query.py", line 1276, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "c:\python27\lib\site-packages\django\db\models\sql\query.py", line 1210, in build_filter
condition = self.build_lookup(lookups, col, value)
File "c:\python27\lib\site-packages\django\db\models\sql\query.py", line 1104, in build_lookup
return final_lookup(lhs, rhs)
File "c:\python27\lib\site-packages\django\db\models\lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "c:\python27\lib\site-packages\django\db\models\lookups.py", line 74, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "c:\python27\lib\site-packages\django\db\models\fields\__init__.py", line 966, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'test'

最佳答案

您犯了常见的 Python 拼写错误之一...

你有 def test(post, self): ,它实际上应该是 def test(self, post): 。它通常发生在进行一些代码重构时......

关于python - 发送 2 个参数到 Celery eta 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57348624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com