multithreading for IO-bound tasks and multiprocessing for CPU-bound tasks

Viewed 3123

https://realpython.com/async-io-python/ gives an introduction about multithreading and multiprocessing, but it does not make clear what is is valid in general or valid only in the Python environment. For instance, it says:

concurrency encompasses both multiprocessing (ideal for CPU-bound tasks) and threading (suited for IO-bound tasks)

I have developed concurrent apps with other programming languages such as C/C++ before and this statement seems odd to me. Why multithreading would not be suited for CPU-bound tasks and multiprocessing for IO-bound tasks in general ? AFAIK both could be used effectively for both tasks. Deciding between both depends on other criteria, such as task granularity, amount of shared state and execution order dependency between tasks and the process/thread creation cost (higher for processes, especially in some OSes). Is the statement above specific to the Python environment and its global lock interpreter limitations?

1 Answers

As Rob Pike, Co-inventor of Go language said:

  • Concurrency is about dealing with lots of things at once.
  • Parallelism is about doing lots of things at once.
  • Not the same but related.
  • One is about structure, one is about execution.
  • Concurrency provides a way to structure a solution to solve a problem that may(but not necessarily) be parallelizable.

From Luciano Ramalho book, "Fluent Python" Chapter 18, page 557.

What they are trying to say is that multiprocessing (it is also the name of the library used in Python for parallelism) is the way to solve issues when you indeed want multiple CPU bound tasks or parallel tasks. In Python, this is achieved by bypassing the GIL using for example the Python Multiprocessing module

In Python, there is something called the GIL that allows only to run one thread at the time. You will need to bypass the GIL to use parallelism. Meanwhile, you can achieve concurrency even with the GIL limitation: Only one thread will run at a time!!

In Python you can achieve concurrency with:

  • Threads,
  • Futures(thread based)
  • and Async I/O ( not thread based, but event loops and cooperative multitask)

So as you can see you have 3 way of concurrency, but because of the GIL limitation you can not use it in Parallel tasks, or CPU-bound tasks

I found an article that could help you with Concurrency and Async I/O

Concurrency in Python with Async I/O

To achieve parallelism in Python you need to bypass the GIL. A python module that helps with that is called "multiprocessing".

Regarding your doubt:

...(which says that multiprocessing is ideal for CPU-bound tasks and multithreading is ideal for IO-bound tasks) only applies to the Python environment..

I can not say if it only applies to Python, as I do not know all the other languages, but for example, Javascript is notorious for his async I/O approach meanwhile C#, C++, Java achieve Concurrency and Parallelism without any inconvenience or limitation using Threads. C# as JavaScript also implemented Async I/O a long time ago.

Both, David Beazley and Łukasz Langa mentioned that fact in the below talks

  • David Beazley, Keynote at PyCon Brazil 2015

  • David Beazley, Curious Course on Coroutines and Concurrency

  • Łukasz Langa, Thinking In Coroutines - PyCon 2016

The links are in the below presentation as well

Related