[Tutor] async learning

Mats Wichmann mats at wichmann.us
Tue Jul 4 19:21:26 EDT 2023


On 7/4/23 16:02, Alan Gauld via Tutor wrote:
> 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.

This one does (fetches from a url, the classic example of a "slow" 
operation in computer terms), but... it's not happening in an async 
function.  As a first step, "add_stuff" needs to be an "async def".
> 
> 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))
> 
> 



More information about the Tutor mailing list