async learning

Leam Hall <[email protected]>
Newsgroups gmane.comp.python.tutor
Message-ID <[email protected]>
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)
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.