I'm writing a program which imports a module using a file path, with the function imp.load_source(module_name,module_path)
. It seems to cause a problem when I try to pass objects from this module into a Process
.
An example:
import multiprocessing
import imp
class MyProcess(multiprocessing.Process):
def __init__(self,thing):
multiprocessing.Process.__init__(self)
self.thing=thing
def run(self):
x=self.thing
if __name__=="__main__":
module=imp.load_source('life', 'C:\\Documents and Settings\\User\\workspace\\GarlicSim\\src\\simulations\\life\\life.py')
thing=module.step
print(thing)
p=MyProcess(thing)
p.start()
Note: for this code to "work", you must substitute the parameters I gave to imp.load_source
with something else: It has to be some Python file on your computer, preferably not in the same folder. Then, in thing=module.step
, instead of step put in some random function or class that is defined in that .py
file.
I am getting the following traceback:
<function step at 0x00D5B030>
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python26\lib\multiprocessing\forking.py", line 342, in main
self = load(from_parent)
File "C:\Python26\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python26\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python26\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Python26\lib\pickle.py", line 1124, in find_class
__import__(module)
ImportError: No module named life
So what do I do?
EDIT:
I'm using Python 2.6.2c1 on Win XP.