我正在构建一个Tornado Web服务器,该服务器需要执行一些阻塞任务,例如压缩视频文件等,这些都非常耗时。理想情况下,我想交出这些任务 到Celery这样的子流程实用程序并相应地通知客户端。所以,我从GitHub& amp;中获取了this代码。修改它以获得更好的理解。我正在使用Ubuntu 12.04 LTS,这是我到目前为止所做的:
apt-get install rabbitmq-server
)。python -m pip install tornado-celery
我的任务代码段代码(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()
我使用celery worker -A myTornadoTasks &
启动任务。在这里一切正常,我看到24个线程正在运行。
我的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
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 -m tcelery --app=myTornadoCelery --address=0.0.0.0
我收到错误AttributeError: 'module' object has no attribute 'celery'
问题:
感谢。
更新了错误:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/dist-packages/tcelery/__main__.py", line 62, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/tcelery/__main__.py", line 56, in main
cmd.execute_from_commandline()
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 309, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
self.app = self.find_app(app)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 489, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/usr/local/lib/python2.7/dist-packages/celery/app/utils.py", line 240, in find_app
found = sym.celery
AttributeError: 'module' object has no attribute 'celery'