Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python:When to use Threads vs. Multiprocessing

Tags:

python

What are some good guidelines to follow when deciding to use threads or multiprocessing when speaking in terms of efficiency and code clarity?

like image 995
John Gann Avatar asked Jan 17 '11 06:01

John Gann


People also ask

When would you want to use multiprocessing vs threading?

Multiprocessing uses two or more CPUs to increase computing power, whereas multithreading uses a single process with multiple code segments to increase computing power. Multithreading focuses on generating computing threads from a single process, whereas multiprocessing increases computing power by adding CPUs.

Should I use multithreading or multiprocessing in Python?

Multiprocessing is a easier to just drop in than threading but has a higher memory overhead. If your code is CPU bound, multiprocessing is most likely going to be the better choice—especially if the target machine has multiple cores or CPUs.

When should you use threads in Python?

Python threads are used in cases where the execution of a task involves some waiting. One example would be interaction with a service hosted on another computer, such as a webserver. Threading allows python to execute other code while waiting; this is easily simulated with the sleep function.

When should I use multiprocessing in Python?

CPU time gets rationed out between the threads. Multiprocessing is for times when you really do want more than one thing to be done at any given time. Suppose your application needs to connect to 6 databases and perform a complex matrix transformation on each dataset.


1 Answers

Many of the differences between threading and multiprocessing are not really Python-specific, and some differences are specific to a certain Python implementation.

For CPython, I would use the multiprocessing module in either fo the following cases:

  • I need to make use of multiple cores simultaneously for performance reasons. The global interpreter lock (GIL) would prevent any speedup when using threads. (Sometimes you can get away with threads in this case anyway, for example when the main work is done in C code called via ctypes or when using Cython and explicitly releasing the GIL where approriate. Of course the latter requires extra care.) Note that this case is actually rather rare. Most applications are not limited by processor time, and if they really are, you usually don't use Python.

  • I want to turn my application into a real distributed application later. This is a lot easier to do for a multiprocessing application.

  • There is very little shared state needed between the the tasks to be performed.

In almost all other circumstances, I would use threads. (This includes making GUI applications responsive.)

like image 146
Sven Marnach Avatar answered Sep 23 '22 22:09

Sven Marnach