AttributeError:'_ MainProcess'对象没有属性'_exiting'

Hel*_*hne 12 python multiprocessing python-2.5

我有一个

AttributeError: '_MainProcess' object has no attribute '_exiting'
Run Code Online (Sandbox Code Playgroud)

来自Python应用程序.不幸的是,这段代码必须运行Python 2.5,因此processing现在称为模块multiprocessing.我正在做的是从主进程创建一个Process带有a Queue和到put队列中的项目.查看processing.queue代码,我可以看到启动了一个支线线程.然后currentProcess()._exiting,该馈线线程将进行检查,但是在模块中可以看到,currentProcess()评估为_MainProcess没有所述属性的线程processing.process.怎么解决这个?这是一个错误processing吗?如果是的话,我可以简单地使用它进行monkeypatch 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()
Run Code Online (Sandbox Code Playgroud)

DrD*_*Dee 1

我不确定为什么在这种情况下你想要 pickle 一个函数,如果你真的想这样做,请看一下这个答案:Is there a simple way to pickle a python function (or other serialize its code)?

否则,这适用于 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()
Run Code Online (Sandbox Code Playgroud)