module path lost in multiprocessing spawn (ModuleNotFoundError)

我与影子孤独终老i 提交于 2021-02-10 17:47:59

问题


Say we have a script that works with no problem:

import module1

class SomeClass(object):
   def __init__(self):
      self.m = module1()

s = SomeCLass()

However if I use multiprocessing module with "spawn" context:

import module1
import multiprocessing as mp

class SomeClass(object):
   def __init__(self):
      self.m = module1()

s = SomeCLass()
ctx = mp.get_context('spawn')
proc = ctx.Process(target=SomeClass, kwargs={})
proc.daemon = True
proc.start()

I get the following error at proc.start():

   ModuleNotFoundError: No module named 'module1'

Yet, if I add the following lines before "import modlule1":

import sys
sys.path.insert(0, path_to_module1)

import module1
import multiprocessing as mp

class SomeClass(object):
   def __init__(self):
      self.m = module1()

s = SomeCLass()
ctx = mp.get_context('spawn')
proc = ctx.Process(target=SomeClass, kwargs={})
proc.daemon = True
proc.start()

it works with no problem. But how can I avoid the sys.path.insert or why does this happen? Is there a more elegant solution?

来源:https://stackoverflow.com/questions/63905636/module-path-lost-in-multiprocessing-spawn-modulenotfounderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!