简体   繁体   中英

Send 2 args to Celery eta task

I use eta to program a task:

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

but I have a task on test.py that take an arg from view, for example 'post':

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

I need to pass 'eta' and 'post', I try with:

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

but give an error:

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

what's wrong?

EDIT1

Try:

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

It try to run task:

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

Result:

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'

You made one of the common Python typos...

You have def test(post, self): where it should really be def test(self, post): . It typically happens when doing some code refactoring...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM