Can someone designed asyncio knows what await do to the function?

event loop run 10000 tasks.

async def task():
    async def func():
            '''takes forever to run the calculation, not doing requests'''

            return something
    result = await func()

When loop meets await, it pause the function1, or not pause, i don’t know, and loop switch to another task - task2 function2.

Question: Does the function 1 running in background?

If YES, it is running with loop - parallelism -MUST ANSWER

If NO, means it is a lame one thread concurrency when 10000 tasks asking time to execute. So function 1 even “paused”, the loop manager will still goes back run it some times (do calculations), check if the calculation is done. Is that correct?

Please don’t answer if you don’t know what am i asking.
if the calculation in the function run and loop manager run to switch to another task at the same time - parallelism.
if its lame one thread, it puts a “pause” tag on the function, so it switch to another task, so the func1 is freezed which will never run, and how it knows it complete when never run it?

Please don’t answer if you just say it is not parallelism.

You can think of event loop as simply a while loop. It’s not running in parallel. Each coroutine is scheduled to run by the event loop one after another.

but what is “await the function to be executed”

It causes the function to yield so that other coroutines can be run.
Once the condition that the function awaited completes the async scheduler will arrange to continue its executions.

The key is that only one coroutine at a time is running.

This is just like the way a generator works by using yield.

1 Like

Yield Statement: Inside a generator function, when you use the yield statement, it temporarily suspends the function’s execution and “yields” a value to the caller. The function’s state is saved, and when you later resume the generator, execution continues from where it was paused.
Yes, it yield the value and other execution inside the function paused -which is infinity paused, the function stop right there.
But when we talk about await, await is waiting for the responses, the function(): return 1+1000, is it executing the same time in the background. Do you know the answer of my question?

No, it isn’t executing in the background. An event loop is only ever running one coroutine at a time.

An image search for “parallelism vs concurrency” might help. Event loops are considered concurrency, but not parallelism.

Imagine pseudo code implementing an event loop like this:

while not cancelled:
    event = get_event_from_queue()
    coroutine = list_of_inactive_passively_awaiting_coroutines[event.coroutine_id]
    activate_the_next_part_of_the_coroutine_until_the_next_await(coroutine, event)
    # the coroutine is inactive again here

Not like this:

while not cancelled:
    event = get_event_from_queue()
    thread = list_of_parallel_active_threads[event.thread_id]
    send_event_to_thread_running_in_parallel(thread, event)
    # continue while all the threads are active in parallel
1 Like

Am I correct that thus would almost work like a decorator which is async, but can’t be used as a decorator because the outer function doesn’t take a fucntion as an argument?

No, a decorator’s job is to transform one function into another function (or in some other way manipulate a function). It does not have anything to do with concurrency or parallelism.