0

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?

2
  • 2
    There's a difference between "parallelism" and "concurrency". Parallelism is a type of concurrency, but concurrency does not necessarily mean parallelism. Concurrency meaning that multiple tasks can be started before the previous task has finished. In pure concurrent systems, portions of the tasks are executed serially and weaved together, but no two actions are performed at the same time (in parallel). Parallelism on the other hand, means that tasks are being executed by multiple workers at the same time (usually through separate cores). Pure Asyncio code is concurrent.
    – flakes
    Commented Aug 6, 2023 at 3:09
  • I've updated the answer - but the answer hasn't essentially changed. If you have a concrete example that doesn't behave the way you expect it to, please share that code, explain the expected behaviour and describe how what happens is different.
    – Grismar
    Commented Aug 7, 2023 at 0:06

1 Answer 1

0

Question: Does the function 1 running in background?

Assuming that by "function 1" you mean task() in the example, which is the function awaiting func(), then no - it does not keep running in the background. You've told it to await the result from func().

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?

I'm not sure what you're asking here, what 10,000 tasks? How do you mean 'lame'? What 'loop manager' and go back to do what calculations? But as previously answered before you rewrote your question: no. There is just one thread here, no parallelism. You're using asynchronicity, not multi-processing.

You seem to not want to hear that answer, but no matter how you ask it, the answer will remain the same.

Look at multiprocessing for actual parallelism, but don't assume parallel execution is automatically faster. There's a lot of overhead involved in running work in parallel on a CPU and your code needs to be well-designed to make good use of it.

3
  • But please answer me this: when await executed, the loop manger put a "pause" tag on the await function but await function is running in the background. What AM I asking here is it writing two program scripts at SAME TIME, which loop manager is running executing other tasks, which will be 500 await functions running at same time, or how it knows that the function is completed as lame one executing some time and other executing some time in one thread?
    – NickDxxD
    Commented Aug 6, 2023 at 2:25
  • I tried reading your comment several times, but I'm afraid it just doesn't make sense. What "pause" tag? What do you mean 'running in the background'? Are you changing your question and do you have something in your code that isn't working as you expected? If that's the case, please update your question, don't change the question from the comments on an answer.
    – Grismar
    Commented Aug 6, 2023 at 3:47
  • result = await func(), it pause the function in loop, give it a "pause" tag.
    – NickDxxD
    Commented Aug 6, 2023 at 6:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.