I am testing toy code to parallelize a process using python's multiprocess. The code works on my home computer but when I migrated it to a remote server I am working on it returns an error.
I first define functions in defs.py
import numpy as np
def g(n):
A = np.random.rand(n, n)
B = np.random.rand(n, n)
return A * B
def run_complex_operations(operation, input, pool):
result = pool.map(operation, input)
return result
Python seems to find defs.py because when I run the two lines below it returns the expected result
import defs
print(defs.g(1))
However, when I run the following code to use my function in a multiprocess, Python returns an error.
import defs
import numpy as np
import time
import multiprocessing as mp
x = 10
n = 10000
l = [n] * x
start = time.time()
if __name__ == '__main__':
processes_pool = mp.Pool(3)
l[:] = defs.run_complex_operations(defs.g, range(x), processes_pool)
The error is:
Process SpawnPoolWorker-1:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 114, in worker
task = get()
File "C:\ProgramData\Anaconda3\lib\multiprocessing\queues.py", line 358, in get
return _ForkingPickler.loads(res)
ModuleNotFoundError: No module named 'defs'
What could be the reasons for the problem? It must be related to multiprocessing because python has no problem finding the other function in the defs module.
FWIW, The server version of python is 3.8.5, my local python is 3.9.7.
import sys; print(sys.path)at the very top of the script and comparing the two.