Python asyncio. Can too many asynchronous calls decrease performance?

Viewed 71

As far as we know, it is bad if we start too many threads, and it may significantly decrease performance and increase memory usage. However, I can't find anywhere if the situation is the same if we call too many async functions.

As far as I know, asyncio is a kind of abstraction for parallel computing, and it may use or may not use actual threading.

In my project, multiple asynchronous tasks are run, and each such task (currently, it is done using threading) may start other threads. It is a risky situation. I'm thinking of two ways how to solve the issue with too many threads. The first one is to limit the number of 'software' threads to the number of 'hardware' threads. Another one is to use asyncio. Is the second option reasonable in such a case?

1 Answers

As far as I know, asyncio is a kind of abstraction for parallel computing and it may use or may not use actual threading.

Please do not confuse parallelism with asynchronous. In Python, you can achieve parallelism only using multiprocessing.

In my project, multiple asynchronous tasks are run, and each such task may start other threads.

All asynchronous tasks are run in one event loop and use only one thread.

I'm thinking of two ways how to solve the issue with too many threads. The first one is to limit the number of 'software' threads to the number of 'hardware' threads. Another one is to use asyncio. Is the second option reasonable in such a case?

In this answer I have demonstrated situations where we can use async functions. It mainly depends on the operations you do. If your application works with threading and does not need multiprocessing, it can be converted to asynchronous tasks.

Related