芹菜融入龙卷风
tornado-celer的Python项目详细描述
Tornado芹菜是Tornado Web框架的非阻塞芹菜客户端
用法
从Tornado请求处理程序调用芹菜任务:
from tornado import gen, web import tcelery, tasks tcelery.setup_nonblocking_producer() class AsyncHandler(web.RequestHandler): @asynchronous def get(self): tasks.echo.apply_async(args=['Hello world!'], callback=self.on_result) def on_result(self, response): self.write(str(response.result)) self.finish()
使用基于生成器的接口调用任务:
class GenAsyncHandler(web.RequestHandler): @asynchronous @gen.coroutine def get(self): response = yield gen.Task(tasks.sleep.apply_async, args=[3]) self.write(str(response.result)) self.finish()
注意:当前回调仅适用于amqp和redis后端。 要使用redis后端,必须安装tornado-redis。
Tornado芹菜可以作为Web服务器启动:
$ cd tornado-celery $ python -m tcelery --port=8888 --app=examples.tasks --address=0.0.0.0
异步执行任务:
$ curl -X POST -d '{"args":["hello"]}' http://localhost:8888/apply-async/examples.tasks.echo/ {"task-id": "a24c9e38-4976-426a-83d6-6b10b4de7ab1", "state": "PENDING"}
得到结果:
$ curl http://localhost:8888/tasks/result/a24c9e38-4976-426a-83d6-6b10b4de7ab1/ {"task-id": "a24c9e38-4976-426a-83d6-6b10b4de7ab1", "state": "SUCCESS", "result": "hello"}
执行任务并得到结果:
$ curl -X POST -d '{"args":[1,2]}' http://localhost:8888/apply/examples.tasks.add/ {"task-id": "fe3cc5a5-d11b-4b17-a6e2-e7fd2fba7ec6", "state": "SUCCESS", "result": 3}
执行超时的任务:
$ curl -X POST -d '{"args":[5],"timeout":1}' http://localhost:8888/apply/examples.tasks.sleep/ {"task-id": "9ca78e26-bbb2-404c-b3bb-bc1c63cbdf41", "state": "REVOKED"}
安装
要安装,只需:
$ pip install tornado-celery
文档
文档可在Read the Docs
运行测试
运行amqp后端的测试:
$ python examples/tasks.py worker $ cd examples && python -m tcelery -A tasks $ python tests/functests.py
要运行redis后端的测试,首先确保redis正在运行,然后:
$ CELERY_RESULT_BACKEND=redis:// python examples/tasks.py worker $ cd examples && CELERY_RESULT_BACKEND=redis:// python -m tcelery -A tasks $ python tests/functests.py