python - Multiprocessing not working with cmd2 module, import issue? -
extending solution question multiprocessing code works upon import, breaks upon being called, have injected multiprocessing code project, has promptly broken.
i think there import issues. have 2 modules. test.py looks like:
print 'omnia praeclara' import multi4 if __name__ == "__main__": multi4.init_manager() print 'rara sunt' and multi4.py looks like:
import multiprocessing mp def add(): print 2+2 def init_manager(): proc = mp.process(target=add) proc.start() proc.join() now, code works fine. produces following when run command line (windows):
omnia praeclara omnia praeclara rara sunt 4 rara sunt which expected (the double printing explained in link above).
however --- when make test.py this:
print 'omnia praeclara' import multi4 if __name__ == "__main__": multi4.init_manager() print 'rara sunt' import cmd2 class prompt(cmd2.cmd): def default(self, line): return cmd2.cmd.default(self, line) prompt = '\n+++ ' intro = '\n remembered or forgot.' def do_exit(self, line): return true prompt().cmdloop() i
omnia praeclara omnia praeclara rara sunt remembered or forgot. +++ this command prompt cmd2 module. process calling add not produce anything. hung now. if type exit prompt get:
+++ exit 4 rara sunt remembered or forgot. +++ so multiprocessing code finishes, back cmd2 prompt! when exit again exit.
clearly, being imported twice. referenced in above link, there way avoid this? more importantly, how can processes working in background while doing things in cmd2?
this same issue that's causing double printing - in test.py not protected if __name__ == "__main__": guard executed in both parent , child processes. need move call prompt().cmdloop() under guard prevent being executed in child:
print 'omnia praeclara' import multi4 print 'rara sunt' import cmd2 class prompt(cmd2.cmd): def default(self, line): return cmd2.cmd.default(self, line) prompt = '\n+++ ' intro = '\n remembered or forgot.' def do_exit(self, line): return true if __name__ == "__main__": multi4.init_manager() prompt().cmdloop()
Comments
Post a Comment