axioman
axioman

Reputation: 131

python pool doesn't work

I'm trying to use multithreading and in order to keep it simple at first, i'm running the following code:

import multiprocessing as mp

pool = mp.Pool(4)

def square(x):

    return x**2

results=pool.map(square,range(1,20))

As i understand, results should be a list containig the squares from 1 to 20. However, the code does not seem to terminate.(doing the same without pool finishes in a blink, this ran for several minutes, before i stopped it manually).

Additional information: task manager tells me, that the additional python processes have been launched and are running, but are using zero % of my cpu; still other unrelated processes like firefox skyrocket in their cpu usage, while the programm is running. I'm using windows 8 and a i5-4300U cpu (pooling to 2 instead of 4 doesn't help either)

What am i doing wrong? Are there any good ressources on the Pool class, which could help me understand what is wrong with my code?

Upvotes: 3

Views: 6481

Answers (2)

Vader
Vader

Reputation: 3873

Code with pool initialization should be inside __name__ == "__main__" as multiprocessing imports the module each time to spawn a new process.

import multiprocessing as mp

def square(x):
    return x**2

if __name__ == '__main__':
    pool = mp.Pool(4)
    results=pool.map(square,range(1,20))

Upvotes: 6

user234461
user234461

Reputation: 1205

Your code causes each process to hit an attribute error because it can't find the square attribute because it wasn't defined when you instantiated the pool. The processes therefore hang thereafter. Defining square before pool would fix the problem.

See also:

yet another confusion with multiprocessing error, 'module' object has no attribute 'f'

Upvotes: 0

Related Questions