Reputation: 275
I have two simple services running on local. (code is below). Why the response coming back in a very long 300 seconds when I send 100 requests at the same time. What is it doing behind the scene?
Service #1, by calling http://localhost:8080
import os
os.environ['PYTHONASYNCIODEBUG'] = '1'
import json
from aiohttp import web
import aiohttp
import asyncio
import importlib
import time
#tasks = []
n = 0
m = 0
def mcowA(m):
print (m, " : A")
return
async def fetch(session, url):
try:
async with session.get(url) as response:
#async with getattr(session,"get")(url,proxy=proxy) as response:
return await response.text()
except Exception:
import traceback
traceback.format_exc()
def mcowB(n):
print (n, " : B")
return
async def runMcows(request):
start = time.time()
global n,m
mcowA(m)
m=m+1
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://localhost:8081')
#html = await fetch(session, 'http://www.cpan.org/SITES.htm
print(n,html)
mcowB(n)
end = time.time()
print ( end - start)
n=n+1
return web.Response(text=html)
async def init():
app = web.Application()
app.add_routes([web.get('/', runMcows)])
return await loop.create_server(
app.make_handler(), '127.0.0.1', 8080)
loop = asyncio.get_event_loop()
loop.run_until_complete(init())
loop.run_forever()
service 2:
from aiohttp import web
import asyncio
import time
async def hello(request):
time.sleep(5)
#await asyncio.sleep(5)
return web.Response(text='dummy done5')
app = web.Application()
app.add_routes([web.get('/', hello)])
web.run_app(app,host='127.0.0.1', port=8081)
I understand the time.sleep(5) is blocking, but why it's blocking 300 seconds? Which part spends the 300 seconds? If changed to await asyncio.sleep(5), it works.
Some of the output: https://github.com/aio-libs/aiohttp/issues/3630
Upvotes: 1
Views: 670
Reputation: 1437
This is blocking.
time.sleep(5)
Asyncio isn't threads. An event loop is running which calls your hello function which blocks for 5 seconds before returning. The event loop then regains control and calls the next event which would be your hello function again which would block for another 5 seconds before returning control to the loop.
This waits async for 5 seconds.
await asyncio.sleep(5)
So your hello function returns immediately and simply tells the loop to come back to me in 5 seconds.
Upvotes: 2