I try to write simple nonblocking http server. I can't manage to all routines (Task1, Task2 and server) run simultaneous. Whatewer I do the server block.
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()
The expected result should be printed on terminal while server runs. But I got terminal output OR server running (commenting second line from end) but want both.
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---