如何在Celery中创建子任务

时间:2015-10-12 18:43:44

标签: python celery

http://celery.readthedocs.org/en/v2.2.6/userguide/tasksets.html

凸出/

celery.py

from __future__ import absolute_import

from kombu import Exchange, Queue
from celery import Celery

app = Celery('proj',
             broker='redis://myredis.com',
             backend='redis://myredis.com',
             include=['proj.tasks'])

a_exchange = Exchange('a_ex', type='topic')

# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
    CELERY_DISABLE_RATE_LIMITS=True,
    CELERY_ROUTES = {"app.tasks.timeme": "a"}
)

if __name__ == '__main__':
    app.start()

tasks.py

from __future__ import absolute_import

from proj.celery import app
import time

@app.task
def timeme(ts):
    print 'hi'
    lat = time.time() - float(ts)
    return (lat, time.time())

do_tasks.py

import proj.tasks
import time
import sys

stime = time.time()
while time.time() < stime + 15:
    res = proj.tasks.timeme.apply_async(args=[time.time()], link=proj.tasks.timeme())

当我运行do_task.py时,它给我一个错误:

TypeError: timeme() takes exactly 1 argument(0 given)

我知道link存在一些问题,因为如果我没有指定link

它会正常工作

我的猜测是timeme必须是一个子任务 我不确定如何将timeme指定为子任务。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

之后我只需要放{。s()

res = proj.tasks.timeme.apply_async(args=[time.time()], link=proj.tasks.timeme.s())