我尝试编写简单的无阻塞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()
服务器运行时,应在终端上打印预期结果。 但是我得到了终端输出或服务器正在运行(从末尾注释第二行),但是两者都想要。
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---
答案 0 :(得分:2)
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())
答案 1 :(得分:0)
我需要做一点修改并使它工作...
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()
答案 2 :(得分:0)
所以这应该是最终解决方案吗?
#!/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())