我正在测试Tornado - 芹菜 - RabbitMQ集成。所以,我从GitHub& amp;中获取了this代码。修改它以获得更好的理解。我正在使用Ubuntu 12.04 LTS,这是我到目前为止所做的:
Installed Tornado and working great on its own.
Installed RabbitMQ (apt-get install rabbitmq-server).
Installed Tornado Celery (downloaded and unzipped the tar file).
Executed python -m pip install tornado-celery
Borrowed 2 example python files from above Github Repo and modified it.
我的任务代码段代码(myTornadoTasks.py)是:
import os
import time
from datetime import datetime
from celery import Celery
from celery import task
celery = Celery("myTornadoTasks", broker="amqp://")
celery.conf.CELERY_RESULT_BACKEND = os.environ.get('CELERY_RESULT_BACKEND', 'amqp')
@celery.task
def add(x, y):
return int(x) + int(y)
..... some more code
@celery.task
def error(msg):
raise Exception(msg)
if __name__ == "__main__":
celery.start()
我使用芹菜工人-A myTornadoTasks --concurrency = 4开始任务。这里一切正常。
我的Tornado Celery代码段(myTornadoCelery.py)是:
from tornado import gen
from tornado import ioloop
from tornado.web import asynchronous, RequestHandler, Application
import myTornadoTasks
import tcelery tcelery.setup_nonblocking_producer()
class AsyncHandler(RequestHandler):
@asynchronous
def get(self):
myTornadoTasks.sleep.apply_async(args=[3], callback=self.on_result)
def on_result(self, response):
self.write(str(response.result))
self.finish()
... some more code
class GenMultipleAsyncHandler(RequestHandler):
@asynchronous
@gen.coroutine
def get(self):
r1, r2 = yield [gen.Task(myTornadoTasks.sleep.apply_async, args=[2]),
gen.Task(myTornadoTasks.add.apply_async, args=[1, 2])]
self.write(str(r1.result))
self.write(str(r2.result))
self.finish()
application = Application([
(r"/async-sleep", AsyncHandler),
(r"/gen-async-sleep", GenAsyncHandler),
(r"/gen-async-sleep-add", GenMultipleAsyncHandler),
])
if __name__ == "__main__":
application.listen(8887)
ioloop.IOLoop.instance().start()
我使用“python myTornadoCelery.py”启动Tornado。没有错误。我打开一个浏览器并输入“http://localhost:8887/gen-async-sleep-add”,浏览器继续旋转。我可以看到Celery收到并执行的任务。我没有在浏览器上看到返回的任务ID等。如果我在GenMultipleAsyncHandler中放入简单的print语句,我看不到在Yield命令之后执行的操作。我必须杀死浏览器。如果我尝试任何其他Tornado请求处理程序,也会发生同样的事情。我做错了什么或错过了什么?
由于