[Tutor] async learning

Leam Hall leamhall at gmail.com
Sun Jul 2 08:21:40 EDT 2023


Until recently, I haven't had a lot of reason to do async stuff. Now I'm trying to figure it out, but I'm missing something. The commented out section is my attempt at async, but it runs slower than the non-async version. Thoughts?

The parameters are:
	Python 3.9 or 3.10
	Import from Standard Library only
	Later, add_stuff() will include running python processes to gather data

Code so far:

import asyncio
import urllib.request

site = "https://en.wikipedia.org/wiki/"

def add_stuff(name, site):
     url = site + name
     result = dict()
     result['name'] = name.replace('_', ' ')
     result['details'] = urllib.request.urlopen(url).read()
     return result

def show_stuff(thing):
     info = thing[1]
     name = info['name']
     details = info['details']
     result = "Let's look at {}, with {} bits and bytes of detail.".format(name, len(details))
     return result

#async def main(names):
#    team = { name: add_stuff(name, site) for name in names}
#    return team

if __name__ == "__main__":
     names = ["Frodo_Baggins", "Samwise_Gamgee", "Merry_Brandybuck", "Pippin_Took"]

     #team = asyncio.run(main(names))
     team = { name: add_stuff(name, site) for name in names}

     for t in sorted(team.items()):
         print(show_stuff(t))



-- 
Software Engineer          (reuel.net/resume)
Scribe: The Domici War     (domiciwar.net)
General Ne'er-do-well      (github.com/LeamHall)


More information about the Tutor mailing list