python - How to use Celery in Tornado with redis as broker and backend? -
it raises exception when try use celery3.1.11, tornado-celery0.3.5 in tornado4.2 yield. works without yield, cannot result asynchronous...i find works when use rabbitmq broker, while redis raise below error...
here code.
from mycelery import celery_task import tcelery tcelery.setup_nonblocking_producer() token = yield tornado.gen.task(celery_task.get_rongcloud_token.apply_async,args=[3]) print token
my celery task:
from celery import celery, platforms celery.schedules import crontab celery.utils.log import get_task_logger celery.exceptions import softtimelimitexceeded platforms.c_force_root = true # linux 下要root用户才不报错 broker = 'redis://:'+settings.redis_pass+'@127.0.0.1:6379/5' backend = 'redis://:'+settings.redis_pass+'@127.0.0.1:6379/6' app = celery('tasks', broker=broker, backend=backend) @app.task(name='mycelery.celery_task.get_rongcloud_token') def get_rongcloud_token(user_id): print 'xxxxx' = 'xxx' return
here error:
typeerror: <function wrapper @ 0x5bd2c80> not json serializable
hha,find same question: tornado celery can't use gen.task or callback
the limited docs tcelery
doesn't explain , example doesn't show either. limited docs, seem you're using intended (unless i'm missing something) i'm not sure why code not working. i've been successful following method:
class celerytasks(web.requesthandler): @gen.coroutine def get(self): future = concurrent.future() celery_task = tasks.get_rongcloud_token.delay(3) check_status(celery_task, future) yield future self.write(future.result()) #self.write(celery_task.result) def check_status(celery_task, future): """ check status of celery task , set result in future """ if not celery_task.ready(): ioloop.ioloop.current().call_later( 1, check_status, celery_task, future) else: future.set_result(celery_task.result)
first bare future
created, yielded until results celery task available. next execute celery task async execution (eg. task_fn.delay(*args)
or task_fn.apply_async(*args)
). pass celery_task
, future
function (check_status
) check if task "ready", if not, recursively call @ later time , check again. yield
future
until result set. once task has completed , result available, set future
result , whatever need result.
Comments
Post a Comment