Issue
I have function:
def foo(page_url: str):
async with ClientSession() as session:
async with session.get(page_url) as response:
if response.status == 200:
page = await response.text()
return (page_url, page)
And it works correct, but i want to make await inside tuple:
return(page_url, await response.text())
my programm immediately starts working much slower.
Why?
Solution
I can't reproduce a drastic slowdown.
- I have
python -m http.server 8254 2>/dev/null
running, so there is no network interference here, and no background terminal being noisy either.
import asyncio
import sys
import aiohttp
async def getter_1(sess: aiohttp.ClientSession, page_url: str):
async with sess.get(page_url) as response:
if response.status == 200:
page = await response.text()
return (page_url, page)
async def getter_2(sess: aiohttp.ClientSession, page_url: str):
async with sess.get(page_url) as response:
if response.status == 200:
return (page_url, await response.text())
def getter_test(getter):
async def test():
async with aiohttp.ClientSession() as sess:
for x in range(3000):
res = await getter(sess, 'http://127.0.0.1:8254/')
assert res
asyncio.run(test())
if sys.argv[1] == '1':
getter_test(getter_1)
else:
getter_test(getter_2)
Running this with the hyperfine
benchmark tool, I get a result
$ hyperfine 'python3 so73542988.py 1' 'python3 so73542988.py 2'
Benchmark 1: python3 so73542988.py 1
Time (mean ± σ): 7.547 s ± 1.547 s [User: 2.228 s, System: 0.298 s]
Range (min … max): 6.417 s … 11.568 s 10 runs
Benchmark 2: python3 so73542988.py 2
Time (mean ± σ): 7.863 s ± 0.524 s [User: 2.217 s, System: 0.297 s]
Range (min … max): 7.218 s … 8.831 s 10 runs
Summary
'python3 so73542988.py 1' ran
1.04 ± 0.22 times faster than 'python3 so73542988.py 2'
so nothing very drastic at all.
Answered By - AKX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.