in file called test.py
have
print 'i cow' import multi4 print 'i cowboy'
and in multi4.py
have
import multiprocessing mp manager = mp.manager() print manager
i confused way code operates.
at command line, if type python
, in python environment if type import test.py
expected behavior:
python 2.7.3 (default, apr 10 2012, 23:31:26) [msc v.1500 32 bit (intel)] on win32 type "help", "copyright", "credits" or "license" more information. >>>import test cow <multiprocessing.managers.syncmanager object @ 0x025209b0> cowboy >>>
however if type test.py
@ command line, get
i cow cow cow cow cow cow cow cow cow cow cow cow cow cow cow cow cow cow
which presumably go on forever unless kill it. when kill it, bunch of repeated errors:
keyboardinterrupt traceback (most recent call last): file "<string>", line 1, in <module> file "c:\python27\lib\multiprocessing\forking.py", line 373, in main prepare(preparation_data) file "c:\python27\lib\multiprocessing\forking.py", line 488, in prepare '__parents_main__', file, path_name, etc keyboardinterrupt traceback (most recent call last): file "<string>", line 1, in <module> file "c:\python27\lib\multiprocessing\forking.py", line 373, in main prepare(preparation_data) file "c:\python27\lib\multiprocessing\forking.py", line 488, in prepare '__parents_main__', file, path_name, etc keyboardinterrupt
so going on? why behave 1 way under import, , when try run it?
multiprocessing
won't work properly in interactive prompt on windows, because can't re-import __main__
in child processes spawns. however, helps here, since keeps manager = mp.manager()
line being recursively executed in child process spawns when manager
starts up.
in actual script, however, child can re-import __main__
properly. you're seeing infinite recursion because you're not protecting call mp.manager()
if __name__ == "__main__":
guard, required on windows prevent mp.manager()
being executed in child when re-import occurs:
import multiprocessing mp if __name__ == "__main__": manager = mp.manager() print manager
edit:
your example, main script (test.py) imports module creates manager
, needs bit of refactoring. need instantiate manager
main script, calling method in module uses multiprocessing
:
print 'i cow' import multi4 if __name__ == "__main__": multi4.init_manager() print 'i cowboy'
multi4.py
import multiprocessing mp manager = none def init_manager(): global manager manager = mp.manager()
this way make sure create manager
when you're executing script.