Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple toy example using multiprocessing module crashes computer

Tags:

python

Trying the very simple following example causes my computer to grind to a halt, so that I have to restart. Checking task manager shows hundreds of "python.exe" tasks:

import math
from multiprocessing import Pool

pool = Pool(processes=2)
print pool.map(math.sqrt, [1,4,9,16])

I am using a dual core cpu (i5 2467m) so I thought the above would be fine.

I tried setting processes=1, which causes a slightly different problem: the task never completes but it does not cause my computer to freeze up.

Any ideas?

like image 546
iRoygbiv Avatar asked Dec 18 '12 14:12

iRoygbiv


People also ask

What is multiprocessing dummy?

multiprocessing. dummy replicates the API of multiprocessing but is no more than a wrapper around the threading module. That means you're restricted by the Global Interpreter Lock (GIL), and only one thread can actually execute CPU-bound operations at a time. That's going to keep you from fully utilizing your CPUs.

When would you use a multiprocessing pool?

Use the multiprocessing. Pool class when you need to execute many short- to modest-length tasks throughout the duration of your application. Use the multiprocessing. Pool class when you need to execute tasks that may or may not take arguments and may or may not return a result once the tasks are complete.

Is multiprocessing faster Python?

This pattern is extremely common, and I illustrate it here with a toy stream processing application. On a machine with 48 physical cores, Ray is 6x faster than Python multiprocessing and 17x faster than single-threaded Python. Python multiprocessing doesn't outperform single-threaded Python on fewer than 24 cores.

How do you write a multiprocessing code?

Example code In this example, at first we import the Process class then initiate Process object with the display() function. Then process is started with start() method and then complete the process with the join() method. We can also pass arguments to the function using args keyword.


1 Answers

I had the same problem the first time I played around with multiprocessing. Wrap the pool generation code in a if __name__ == '__main__' block.

import math
from multiprocessing import Pool

if __name__ == '__main__':
    pool = Pool(processes=2)
    print pool.map(math.sqrt, [1,4,9,16])

What's happening is that when the subprocess is created, your module is being unpickled and rerunning the pool generation code in an infinite recursion. With the if block, the spawning code will only run in the parent instance of the module.

like image 138
Silas Ray Avatar answered Dec 01 '22 00:12

Silas Ray