tornado_celery.py
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
from celery_app import async_consume
class MainHandler(tornado.web.RequestHandler):
async def get(self):
async_consume.delay()
self.write('success')
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8888)
print('start')
tornado.ioloop.IOLoop.current().start()
celery_app.py
# -*- coding: utf-8 -*-
import asyncio
from asyncio import sleep
from celery import Celery
app = Celery('tornado')
app.conf.broker_url = 'redis://localhost:6379/0'
app.conf.result_backend = 'redis://localhost:6379/1'
async def consume():
await sleep(3)
return 'test'
@app.task(name='tornado')
def async_consume():
#核心代码asyncio.run
return asyncio.run(consume())