Skip to content
Advertisement

How do I uses Python’s multiprocessing.Value method with .map()?

I’m testing some code using multiprocessing to try to understand it, but I’m struggling to get the .Value to work. What am I doing wrong, it says p doesn’t exist?

here’s my code:

JavaScript

And I get this error:

JavaScript

What should I do?

Advertisement

Answer

You only want a single instance of the shared value, p, to be operated on by the pool processes executing your worker function, addone. The way to do this is to use the initalizer and initargs arguments of the multiprocessing.pool.Pool class to initialize a global variable, p, in each pool process with the shared value created by the main process. Passing explicitly p to a multiprocessing pool worker function as an additional argument will not work; this will result in the cryptic error: RuntimeError: Synchronized objects should only be shared between processes through inheritance.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement