Python 非阻塞服务器
Python nonblocking server
我尝试编写简单的非阻塞 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()
预期结果应在服务器 运行 时打印在终端上。
但是我得到了终端输出或服务器 运行ning(从末尾开始评论第二行),但两者都想要。
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_app
是一个方便的函数,它设置服务器 并且 运行事件循环直到服务器关闭。它是一个用于简单示例的同步函数,因此不打算传递给 create_task
。 create_task
没有引发异常的唯一原因是因为 run_app
从来没有 returns,所以最后的 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())
我只需要做一点修改就可以正常工作了...
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)
async def HttspServer():
# create the application, as before
app = web.Application()
app.add_routes([
web.get('/', handle),
web.get('/{name}', handle)
])
# set up the web server
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner)
await site.start()
# wait forever, running both the web server and the tasks
await asyncio.Event().wait()
loop=asyncio.new_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
loop.create_task(HttspServer())
loop.run_forever()
所以这应该是最终解决方案?
#!/usr/bin/python
# -*- coding: utf8 -*-
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):
data = {'some': 'data'}
return web.json_response(data)
async def main():
# create the application, as before
app = web.Application()
app.add_routes([
web.get('/', handle),
web.get('/{name}', handle)
])
# add some tasks into the current event loop
loop = asyncio.get_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
# set up the web server
runner = web.AppRunner(app)
await runner.setup()
await web.TCPSite(runner).start()
# wait forever, running both the web server and the tasks
await asyncio.Event().wait()
asyncio.get_event_loop().run_until_complete(main())
我尝试编写简单的非阻塞 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()
预期结果应在服务器 运行 时打印在终端上。 但是我得到了终端输出或服务器 运行ning(从末尾开始评论第二行),但两者都想要。
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_app
是一个方便的函数,它设置服务器 并且 运行事件循环直到服务器关闭。它是一个用于简单示例的同步函数,因此不打算传递给 create_task
。 create_task
没有引发异常的唯一原因是因为 run_app
从来没有 returns,所以最后的 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())
我只需要做一点修改就可以正常工作了...
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)
async def HttspServer():
# create the application, as before
app = web.Application()
app.add_routes([
web.get('/', handle),
web.get('/{name}', handle)
])
# set up the web server
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner)
await site.start()
# wait forever, running both the web server and the tasks
await asyncio.Event().wait()
loop=asyncio.new_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
loop.create_task(HttspServer())
loop.run_forever()
所以这应该是最终解决方案?
#!/usr/bin/python
# -*- coding: utf8 -*-
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):
data = {'some': 'data'}
return web.json_response(data)
async def main():
# create the application, as before
app = web.Application()
app.add_routes([
web.get('/', handle),
web.get('/{name}', handle)
])
# add some tasks into the current event loop
loop = asyncio.get_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
# set up the web server
runner = web.AppRunner(app)
await runner.setup()
await web.TCPSite(runner).start()
# wait forever, running both the web server and the tasks
await asyncio.Event().wait()
asyncio.get_event_loop().run_until_complete(main())