Py学习  »  tornado

结合Asyncio和Tornado

Chris • 6 年前 • 2555 次点击  

在龙卷风的第五版中 tornado.ioloop.IOLoop.current() 异步事件循环是否可用-如何确保从处理程序调用的aiohttp web抓取脚本使用相同的事件循环?

有没有这样的例子?

谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43810
文章 [ 1 ]  |  最新文章 6 年前
Ben Darnell
Reply   •   1 楼
Ben Darnell    6 年前

它应该只是默认的工作。下面是一个简单的例子:

from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application
import aiohttp

class MyHandler(RequestHandler):
    async def get(self):
        async with aiohttp.ClientSession() as session:
            async with session.get("https://www.google.com/robots.txt") as resp:
                self.write(await resp.text())

app = Application([('/', MyHandler)])
app.listen(8080)
IOLoop.current().start()