Python如何在类内部进行多处理? [英] Python how to do multiprocessing inside of a class?

查看:84
本文介绍了Python如何在类内部进行多处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的代码结构:

I have a code structure that looks like this:

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

    # Start all processes
    [x.start() for x in processes]

  def RunProcess(self, i):
    do something with i...

主脚本:

myA = A()

我似乎无法运行它.我收到运行时错误" 当前过程已完成其引导阶段."

I can't seem to get this to run. I get a runtime error "An attempt has been made to start a new process before the current process has finished its bootstrapping phase."

如何为此进行多个处理?如果我使用Threading,它可以正常工作,但它的速度和顺序一样慢...而且我还担心多重处理也会很慢,因为创建该过程需要更长的时间?

How do I get multiple processing working for this? If I use Threading, it works fine but it is as slow as sequential... And I'm also afraid that multiple processing will also be slow because it takes longer for the the process to be created?

有什么好的秘诀吗?预先非常感谢.

Any good tips? Many thanks in advance.

推荐答案

在您的代码中我可以看到一些语法问题:

There are a couple of syntax issues that I can see in your code:

    Process中的
  • args需要一个元组,您传递一个整数,请将第5行更改为:

  • args in Process expects a tuple, you pass an integer, please change line 5 to:

p = Process(target=self.RunProcess, args=(i,))

list.append是一种方法,传递给它的参数应包含在()中,而不是[]中,请将第6行更改为:

list.append is a method and arguments passed to it should be enclosed in (), not [], please change line 6 to:

processes.append(p)

正如@qarma指出的那样,在类构造函数中启动进程不是一个好习惯.我将代码结构如下(适应您的示例):

As @qarma points out, its not good practice to start the processes in the class constructor. I would structure the code as follows (adapting your example):

import multiprocessing as mp
from time import sleep

class A(object):
    def __init__(self, *args, **kwargs):
        # do other stuff
        pass

    def do_something(self, i):
        sleep(0.2)
        print('%s * %s = %s' % (i, i, i*i))

    def run(self):
        processes = []

        for i in range(1000):
            p = mp.Process(target=self.do_something, args=(i,))
            processes.append(p)

        [x.start() for x in processes]


if __name__ == '__main__':
    a = A()
    a.run()

这篇关于Python如何在类内部进行多处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆