12

我有一个

AttributeError: '_MainProcess' object has no attribute '_exiting'

来自 Python 应用程序。不幸的是,这段代码必须运行 Python 2.5,因此processing现在称为multiprocessing. 我正在做的是从主进程创建一个Process带有Queueput的队列中的一个项目。查看processing.queue代码,我可以看到一个馈线线程已启动。然后,该馈线线程将检查currentProcess()._exiting,但currentProcess()评估为_MainProcess不具有所述属性的 a ,如processing.process模块中所示。如何解决这个问题?它是一个错误processing吗?如果是,我可以简单地使用猴子补丁currentProcess()._exiting = False吗?

最小的例子:

#!/usr/bin/python

import processing
import processing.queue

class Worker(processing.Process):
    def __init__(self):
        processing.Process.__init__(self)
        self.queue = processing.queue.Queue()

    def run(self):
        element = self.queue.get()
        print element

if __name__ == '__main__':
    w = Worker()
    w.start()
    # To trigger the problem, any non-pickleable object is to be passed here.
    w.queue.put(lambda x: 1)
    w.join()
4

1 回答 1

1

我不确定在这种情况下为什么要腌制一个函数,如果你真的想这样做,看看这个答案:是否有一种简单的方法可以腌制一个 python 函数(或以其他方式序列化它的代码)?

否则,这适用于 python 2.6(我知道你正在寻找 2.5,但我没有 2.5)。我已将您的 lambda 函数替换为常规函数,并将其提供给处理构造函数:

from multiprocessing import Process, Queue

def simple():
    return 1

class Worker(Process):
    def __init__(self, args):
        Process.__init__(self, args=args)
        self.queue = Queue()

    def run(self):
        element = self.queue.get()
        print element

if __name__ == '__main__':
    w = Worker(args=[simple])
    w.start()
    w.join()
于 2011-02-25T17:23:24.773 回答