您好,登錄后才能下訂單哦!
celery是實現一個簡單,靈活可靠的分布式任務隊列系統的好選擇
tornado則不用過多介紹
在開發機上安裝rabbitmq這里就不介紹了
首先是task文件的編寫
task.py
#coding=utf-8 from celery import Celery from celery.bin import worker as celery_worker import celeryconfig broker = 'amqp://' backend = 'amqp' app = Celery('celery_test', backend=backend, broker=broker) app.config_from_object(celeryconfig) @app.task def mytask0(task_name): print "task0:%s" %task_name return task_name @app.task def mytask1(task_name): print "task1:%s" %task_name return task_name def worker_start(): worker = celery_worker.worker(app=app) worker.run(broker=broker, concurrency=4, traceback=False, loglevel='INFO') if __name__ == "__main__": worker_start()
celeryconfig.py文件中包含對celery的配置
#coding=utf-8 from kombu import Queue CELERY_DEFAULT_QUEUE = 'mytask0' CELERY_QUEUES = ( Queue('mytask0', routing_key='task.mytask0'), Queue('mytask1', routing_key='task.mytask1'), ) CELERY_DEFAULT_EXCHANGE_TYPE = 'direct' CELERY_DEFAULT_ROUTING_KEY = 'task.mytask0' CELERY_TIMEZONE = 'Asia/Shanghai' CELERY_ROUTES = { 'task.mytask0': { 'queue': 'mytask0', 'routing_key': 'task.mytask0', }, 'task.mytask1': { 'queue': 'mytask1', 'routing_key': 'task.mytask1', }, }
執行python task.py將會啟動worker
tornado調用celery將阻塞任務變為非阻塞
這會使用到tcelery模塊,即tornado下的一個非阻塞的broker實現
app.py
#coding=utf-8 from tornado import web import task class TestHandler(tornado.web.RequestHandler): @web.asynchronous def get(self): task.mytask0.apply_async( args=['task0'], queue='mytask0', routing_key='task.mytask0', callback=self.on_success) def on_success(self, result): self.finish({'task':result.result})
start.py
用于實現tornado服務的啟動
#coding=utf-8 import tornado from tornado.options import define, options, parse_command_line from tornado.log import enable_pretty_logging import tcelery from app import TestHandler import tornado.httpserver define("port", default=8000, help="run on the given port", type=int) define("debug", default=False, help="run in debug mode") urls = [(r"/api/task/test", TestHandler)] def server_start(): app = tornado.web.Application(urls, debug=options.debug) enable_pretty_logging() parse_command_line() server = tornado.httpserver.HTTPServer(app) server.bind(options.port) server.start(2) tcelery.setup_nonblocking_producer(limit=2) tornado.ioloop.IOLoop.current().start() if __name__ == "__main__": server_start()
執行python start.py即可啟動服務
以上這篇tornado+celery的簡單使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。