Raymond Hettinger gave a talk on concurrency in python, where one of examples looked like that:
import urllib.request
sites = [
'https://www.yahoo.com/',
'http://www.cnn.com',
'http://www.python.org',
'http://www.jython.org',
'http://www.pypy.org',
'http://www.perl.org',
'http://www.cisco.com',
'http://www.facebook.com',
'http://www.twitter.com',
'http://www.macrumors.com/',
'http://arstechnica.com/',
'http://www.reuters.com/',
'http://abcnews.go.com/',
'http://www.cnbc.com/',
]
for url in sites:
with urllib.request.urlopen(url) as u:
page = u.read()
print(url, len(page))
Essentially we go after these links and print amount of received bytes and it takes about 20 seconds to run.
Today I found trio library that has quite friendly api. But when I am trying to use it with this rather basic example I am failing to do it right.
first try (runs around the same 20 seconds):
import urllib.request
import trio, time
sites = [
'https://www.yahoo.com/',
'http://www.cnn.com',
'http://www.python.org',
'http://www.jython.org',
'http://www.pypy.org',
'http://www.perl.org',
'http://www.cisco.com',
'http://www.facebook.com',
'http://www.twitter.com',
'http://www.macrumors.com/',
'http://arstechnica.com/',
'http://www.reuters.com/',
'http://abcnews.go.com/',
'http://www.cnbc.com/',
]
async def show_len(sites):
t1 = time.time()
for url in sites:
with urllib.request.urlopen(url) as u:
page = u.read()
print(url, len(page))
print("code took to run", time.time() - t1)
if __name__ == "__main__":
trio.run(show_len, sites)
and second one (same speed):
import urllib.request
import trio, time
sites = [
'https://www.yahoo.com/',
'http://www.cnn.com',
'http://www.python.org',
'http://www.jython.org',
'http://www.pypy.org',
'http://www.perl.org',
'http://www.cisco.com',
'http://www.facebook.com',
'http://www.twitter.com',
'http://www.macrumors.com/',
'http://arstechnica.com/',
'http://www.reuters.com/',
'http://abcnews.go.com/',
'http://www.cnbc.com/',
]
async def link_user(url):
with urllib.request.urlopen(url) as u:
page = u.read()
print(url, len(page))
async def show_len(sites):
t1 = time.time()
for url in sites:
await link_user(url)
print("code took to run", time.time() - t1)
if __name__ == "__main__":
trio.run(show_len, sites)
So how is this example should be dealt with using trio?