1

我对多处理有问题。在你下面有代码(他在几个类和文件中,但我简化了它)。我想,这个问题在于我想在多处理中相乘的传递方法名称。
信息:“args”是一个列表,如 [(0,1),(1,2),(2,3)] 所以单个“arg”如 (0,1) 这两个文件完全在其他 calatogs 中
! !第一个文件!!

from ... import EF
from ... import someclass

class performs():
    def action():
        for i, arg in enumerate(args):
            data.append(EF(self.method,list(arg),i))
        someclass.create_processes(*data)

    def method(self,fa,la):
        ...

!!第二个文件!!

from multiprocessing import Process,Event
class EF(object):
    def __init__(self,name,args=list(),proc=1):
        self.name=name
        self.args=args
        self.proc=proc

class someclass:
    @staticmethod
    def create_processes(*functions):
        processes=dict()
        for function in functions:
            process=Process(target=function.name,args=function.args)
            process.start()
            processes[process.pid]=process
        for process in processes.values():
            process.join()

当我调试时,错误来了,当程序执行这个指令“process.start()”

安慰:

File "C:\Python32\lib\multiprocessing\forking.py", line 371, in main
    self = load(from_parent)
AttributeError: 'performs' object has no attribute 'function'

或在其他情况下

File "C:\Python32\lib\multiprocessing\process.py", line 267, in _bootstrap
    self.run()
  File "C:\Python32\lib\multiprocessing\process.py", line 116, in run
    self._target(*self._args, **self._kwargs)
  File "...\performs.py", line 88, in method
...

我不知道这很重要,但我有 64 位系统,并安装了 32 位的 Python 和附件

4

1 回答 1

0

评论框太小了,但您的代码似乎运行良好。我通过将它复制到两个文件(如您的环境)中来测试它,但它当然也适用于单个文件。您可以在下面找到该文件:它按预期工作(除了打印语句输出被混淆,但这是意料之中的)。

那么,很可能,您的问题出在其他地方?该错误似乎表明可能有一些包含顺序,如this question中所述?

from multiprocessing import Process,Event

class EF:
    def __init__(self, name, args=list(), proc=1):
        self.name = name
        self.args = args
        self.proc = proc

class someclass:
    @staticmethod
    def create_processes(*functions):
        processes=dict()
        for function in functions:
            process=Process(target=function.name,args=function.args)
            process.start()
            processes[process.pid]=process
        for process in processes.values():
            process.join()

class performs:
    def action(self, args):
        data = []
        for i, arg in enumerate(args):
            data.append(EF(self.mult, list(arg), i))
        someclass.create_processes(*data)

    def mult(self,fa,la):
        print '%d x %d = %d' % (fa, la, fa * la)

if __name__ == '__main__':
    p = performs()
    p.action([(x, x+1) for x in xrange(10)])
于 2012-10-19T11:19:56.807 回答