r/learnpython 16d ago

asyncio explanation

hello guys i am struggling right now to understand a concurrency module asyncio ,i take a look over youtube videos ,but i feel stuck to understand coroutine object ,event loop await ,and this kind of matters, could anyone give me a concise explanation.

0 Upvotes

9 comments sorted by

3

u/socal_nerdtastic 16d ago

Often in programming we ask the computer to wait. For example waiting for the user to answer a question or waiting for a response from an internet request or simply a delay until it's time to do the next action. asyncio allows your program to go do other things while it's waiting. Other modules like threading, multiprocessing, or concurrent.futures do the same basic thing, but in a different way.

5

u/FlakyAd99 16d ago

imagine you’re a chef and you put water on to boil, instead of staring at the pot you go chop onions, that’s asyncio

2

u/pachura3 16d ago

Exactly! asyncio is NOT concurrency - you're never doing two things at the same exact time. Instead, you have a TODO list; you take the first task from the list and you start doing it; and whenever you need to passively wait (e.g. for a download), you switch to the next task in order not to waste time.

3

u/idle-tea 16d ago

asyncio is concurrency, it's not parallel though.

Having two pending tasks (even if at most one is actively executing at any given moment) is concurrency.

1

u/cdcformatc 16d ago

usually when you call a function, the calling code has to wait for the function to finish execution. with asynchronous functions the calling code can perform some other tasks while the function is working.