Multiprocessing.Pool

suggest change

The simple answer, when asking how to use threads in Python is: “Don’t. Use processes, instead.” The multiprocessing module lets you create processes with similar syntax to creating threads, but I prefer using their convenient Pool object.

Using the code that David Beazley first used to show the dangers of threads against the GIL, we’ll rewrite it using multiprocessing.Pool:

David Beazley’s code that showed GIL threading problems

from threading import Thread
import time
def countdown(n):
    while n > 0:
        n -= 1

COUNT = 10000000

t1 = Thread(target=countdown,args=(COUNT/2,))
t2 = Thread(target=countdown,args=(COUNT/2,))
start = time.time()
t1.start();t2.start()
t1.join();t2.join()
end = time.time()
print end-start

Re-written using multiprocessing.Pool:import multiprocessing import time def countdown(n): while n > 0: n -= 1 COUNT = 10000000 start = time.time() with multiprocessing.Pool as pool: pool.map(countdown, [COUNT/2, COUNT/2]) pool.close() pool.join() end = time.time() print(end-start)Instead of creating threads, this creates new processes. Since each process is its own interpreter, there are no GIL collisions. multiprocessing.Pool will open as many processes as there are cores on the machine, though in the example above, it would only need two. In a real-world scenario, you want to design your list to have at least as much length as there are processors on your machine. The Pool will run the function you tell it to run with each argument, up to the number of processes it creates. When the function finishes, any remaining functions in the list will be run on that process.I’ve found that, even using the with statement, if you don’t close and join the pool, the processes continue to exist. To clean up resources, I always close and join my pools.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Working around Global Interpreter Lock:
* Multiprocessing.Pool

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
107 Working around Global Interpreter Lock
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve