Python 非阻塞服务器

eSl*_*vko 4 python-3.x python-asyncio aiohttp

我尝试编写简单的非阻塞 http 服务器。我无法管理所有例程(Task1、Task2 和服务器)同时运行。不管我做什么服务器块。

import asyncio
from aiohttp import web


async def Task1():
    for i in range(100):
        print ('Task-1',i)
        await asyncio.sleep(1)


async def Task2():
    for i in range(100):
        print ('Task-2',i)
        await asyncio.sleep(2)

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)



app = web.Application()
app.add_routes([web.get('/', handle), web.get('/{name}', handle)])


loop=asyncio.new_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
loop.create_task(web.run_app(app)) #with this line commented task1/2 works
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)

服务器运行时,应在终端上打印预期结果。但是我得到了终端输出或服务器正在运行(从末尾开始评论第二行)但两者都想要。

Task-1 0
Task-2 0
Task-1 1
Task-2 1
Task-1 2
Task-1 3
Task-2 2
Task-1 4
Task-1 5
Task-2 3
Task-1 6
---more--- 
Run Code Online (Sandbox Code Playgroud)

use*_*342 5

run_app是一个方便的函数,用于设置服务器运行事件循环,直到服务器关闭。它是一个用于简单示例的同步函数,因此不打算传递给create_task. create_task不引发异常的唯一原因是因为run_app从不返回,所以最终create_task实际上并没有被调用。

要获得对事件循环的控制并向其添加其他任务,您可以使用AppRunner. 例如(未经测试):

async def main():
    # create the application, as before
    app = aiohttp.web.Application()
    app.add_routes([
        aiohttp.web.get('/', handle),
        aiohttp.web.get('/{name}', handle)
    ])

    # add some tasks into the current event loop
    asyncio.create_task(Task1())
    asyncio.create_task(Task2())

    # set up the web server
    runner = aiohttp.web.AppRunner(app)
    await runner.setup()
    await aiohttp.web.TCPSite(runner).start()

    # wait forever, running both the web server and the tasks
    await asyncio.Event().wait()

asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)