Python's asyncio and threading

Targo_ report abuse

I want to learn more about advanced Python topics. At the moment, I am focused on how parallelism is realised in Python. I found information that there are such libraries as asyncio and threading in Python, that allow parallelizing the execution of the code. But I wonder what is the difference between them. Can somebody explain to me this, please?


label report abuse __ edited

Actually, the threading and asyncio libraries are the packages for concurrency in Python, not for parallelism. Concurrency and parallelism are slightly different concepts, try google for them. Back to your question, threading is implementing concurrency by creating several threads, while asyncio has just one thread where many tasks operate.

cwizards report abuse

asyncio usually is faster than threading, because to start and set up a thread Python needs more time than to start and set up a task within the single thread. So, asyncio can be considered as a default choice if you need to implement concurrency in Python nowadays.

label report abuse

I agree with you, @cwizards, but threading has convenient syntactical sugar called Executors. They simplify the development significantly, you don't need to think about many low-level things and avoid some nasty bugs that often occur during the concurrency implementation. asyncio doesn't have such cool features yet. In addition, there are await and async keywords that should be placed in the correct place in your code. Otherwise, the code will not work. So, asyncio is faster but can be considered as more complex at the moment. If you don't need super speed in your program, you can use threading to simplify the development.

Targo_ report abuse

Thank you!

Add Answer

Need support?

Just drop us an email to ... Show more