简体   繁体   English

python aiohttp 进入现有的事件循环

[英]python aiohttp into existing event loop

I'm testing aiohttp and asyncio.我正在测试 aiohttp 和 asyncio。 I want the same event loop to have a socket, http server, http client.我希望相同的事件循环具有套接字、http 服务器、http 客户端。

I'm using this sample code:我正在使用此示例代码:

@routes.get('/')
async def hello(request):
    return web.Response(text="Hello, world")

app = web.Application()
app.add_routes(routes)
web.run_app(app)

The problem is run_app is blocking.问题是run_app被阻塞了。 I want to add the http server into an existing event loop, that I create using:我想将 http 服务器添加到我使用以下方法创建的现有事件循环中:

asyncio.get_event_loop()

The problem is run_app is blocking.问题是run_app被阻塞了。 I want to add the http server into an existing event loop我想将 http 服务器添加到现有的事件循环中

run_app is just a convenience API. run_app只是一个方便的 API。 To hook into an existing event loop, you can directly instantiate the AppRunner :要挂钩现有的事件循环,您可以直接实例化AppRunner

loop = asyncio.get_event_loop()
# add stuff to the loop
...

# set up aiohttp - like run_app, but non-blocking
runner = aiohttp.web.AppRunner(app)
loop.run_until_complete(runner.setup())
site = aiohttp.web.TCPSite(runner)    
loop.run_until_complete(site.start())

# add more stuff to the loop
...

loop.run_forever()

In asyncio 3.8 and later you can use asyncio.run() :在 asyncio 3.8 及更高版本中,您可以使用asyncio.run()

async def main():
    # add stuff to the loop, e.g. using asyncio.create_task()
    ...

    runner = aiohttp.web.AppRunner(app)
    await runner.setup()
    site = aiohttp.web.TCPSite(runner)    
    await site.start()

    # add more stuff to the loop, if needed
    ...

    # wait forever
    await asyncio.Event().wait()

asyncio.run(main())

For the future traveler from Google, here is a simpler way.对于来自 Google 的未来旅行者,这里有一个更简单的方法。

async def main():
    await aio.gather(
        web._run_app(app, port=args.port),
        SomeotherTask(),
        AndAnotherTask()
    )

aio.run(main())

Explanation: web.runapp is a thin wrapper over internal function web._runapp .说明: web.runapp是对内部函数web._runapp的薄包装。 The function uses the old style way of getting the eventloop and then calling loop.run_until_complete .该函数使用旧式方式获取loop.run_until_complete ,然后调用loop.run_until_complete

We replace it with aio.gather alongside other tasks that we want to run concurrently and use the aio.run to schedule them我们将其替换为aio.gather以及我们想要并发运行的其他任务,并使用aio.run来安排它们

Source 来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asyncio create task and aiohttp, 'no running event loop' - asyncio create task and aiohttp , 'no running event loop' “运行时错误:此事件循环已在运行”; 在 python 3.6.5 中调试 aiohttp、asyncio 和 IDE“spyder3” - “RuntimeError: This event loop is already running”; debugging aiohttp, asyncio and IDE “spyder3” in python 3.6.5 Python 问题:无法在单独的线程或事件循环中运行连接 aiohttp 服务器 - Python Problems: Unable to run connexion aiohttp server in a separate thread or event loop Python-无法与aiohttp一起使用多个事件循环 - Python - Unable to use multiple event loops with aiohttp 结合 asyncio 和 aiohttp 时出现“RuntimeError:此事件循环已在运行” - "RuntimeError: This event loop is already running" when combine asyncio and aiohttp Flask asyncio aiohttp - RuntimeError:线程'Thread-2'中没有当前事件循环 - Flask asyncio aiohttp - RuntimeError: There is no current event loop in thread 'Thread-2' 在同一代码中与另一个事件循环一起运行 aiohttp - Run aiohttp alongside another event loop in the same code 使用现有的 asyncio 事件循环在 Python 中实现 REST API - Implementing REST API in Python with existing asyncio event loop asyncio/aiohttp - create_task() 阻止事件循环,在“此事件循环已在运行”中收集结果 - asyncio/aiohttp - create_task() blocks event loop, gather results in “This event loop is already running ” 如何在Python中使用aiohttp或asyncio创建并行循环? - How to create a parallel loop using aiohttp or asyncio in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM