Python how to do multiprocessing inside of a class?

后端 未结 3 688
余生分开走
余生分开走 2021-02-01 03:43

I have a code structure that looks like this:

Class A:
  def __init__(self):
    processes = []
    for i in range(1000):
      p = Process(target=self.RunProces         


        
3条回答
  •  梦如初夏
    2021-02-01 04:23

    A practical work-around is to break down your class, e.g. like this:

    class A:
        def __init__(self, ...):
            pass
    
        def compute(self):
            procs = [Process(self.run, ...) for ... in ...]
            [p.start() for p in procs]
            [p.join() for p in procs]
    
        def run(self, ...):
            pass
    
    pool = A(...)
    pool.compute()
    

    When you fork a process inside __init__, the class instance self may not be fully initialised anyway, thus it's odd to ask a subprocess to execute self.run, although technically, yes, it's possible.

    If it's not that, then it sounds like an instance of this issue:

    http://bugs.python.org/issue11240

提交回复
热议问题