1

I have been using asyncio for a while together with multiprocessing and threading in python. But I'm not sure if i can see the diference between AsyncIO and threading in python.

Python is always running single process (unless multiprocessing is used) and thats exactly why asyncio and multithreading is good (both are switching between tasks). Multiple threads cant be executed concurently in python, so they are being switched all the time. Thats the same as asyncio isn't it ?

Can anyone please provide me why may be one of them better for some situations and why ?

0

1 Answer 1

1

In a nutshell:

  • Multithreading is a concurrency tool facility implemented at the OS level. It usually uses preemptive multitasking , i.e. concurrent tasks can be suspended anywhere to let others running.
  • Asynchronous programming (AsyncIO) is a concurrency facility implemented at the program level. It usually uses cooperative multitasking, i.e. concurrent tasks can be suspended only at specific points marked with a special directive (generally await).

Please note that this is a very simplistic description and the designs can vary a lot. Multithreading is generally used to provide concurrency between programs and to provide parallelism while asynchronous programming shines in programs that schedule lots of concurrent IO (hence the name AsyncIO).

Python is special in that its multi-threading capabilities are contained by the GIL: only one thread can run at a time. This obviously limits a lot the usefulness of multithreading in Python.

Threads provide a lower-level degree of concurrency than tasks (the unit of concurrency in asynchronous programming) but are more heavy-weight.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.