I have some code that is basically a big for loop, running a lot of things on several groups of data and outputting them individually. As they don't rely on each other, I thought I could use multiprocessing to run some of the loops simultaneously to save time. I've looked into the multiprocessing module in python, and tried running a few examples to try and understand how it works, however every example I run gives the same error, and doesn't complete running. For example, the below from the multiprocessing documentation:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
Returns the below error and doesn't output (I've removed my file paths for privacy reasons):
Process SpawnPoolWorker-1:
Traceback (most recent call last):
File "*filepath*", line 315, in _bootstrap
self.run()
File "*filepath*", line 108, in run
self._target(*self._args, **self._kwargs)
File "*filepath*", line 114, in worker
task = get()
File "*filepath*", line 358, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
Process SpawnPoolWorker-2:
Traceback (most recent call last):
File "*filepath*", line 315, in _bootstrap
self.run()
File "*filepath*", line 108, in run
self._target(*self._args, **self._kwargs)
File "*filepath*", line 114, in worker
task = get()
File "*filepath*", line 358, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
Process SpawnPoolWorker-3:
Traceback (most recent call last):
File "*filepath*", line 315, in _bootstrap
self.run()
File "*filepath*", line 108, in run
self._target(*self._args, **self._kwargs)
File "*filepath*", line 114, in worker
task = get()
File "*filepath*", line 358, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
I've also tried running many different examples from places like python guides and Geeks for Geeks and get the same/similar errors on every example I've found.
I know others have had issues with this, and have found the following SO questions, but none of the answers have got me any closer to understanding what's going on and how to use this module:
I'm at a bit of a loss with this, as I can't start trying to figure out if/how this module could help in my example, if I can't get the examples online to work. I'm writing this in spyder.