[Tutor] async learning

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jul 4 18:02:26 EDT 2023


On 02/07/2023 13:21, Leam Hall wrote:
> Until recently, I haven't had a lot of reason to do async stuff. ...> but it runs slower than the non-async version. Thoughts?

I'm no expert but trivial tasks often run slower on async
architecture than when in a single process, even a single
thread.

I don;t know how you measured things or what kind of tasks
you were doing, but if a single task takes less than a few
milliseconds to execute it's likely that the overheads in
async are higher than the benefits of concurrency.

Try creating a task that takes some time such as opening a
file and processing its contents in some
way(search/sort/convert/calculate something per line, say). Then create
many such
files and have your code process them all. That should show
a gain of some sort.

Concurrency is hard! And the right kind of concurrency
depends greatly on the problem. async is best suited to
server type scenarios.


> 
> 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))


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list