AttributeError: '_MainProcess'对象没有'_exiting'属性
我在一个Python应用程序中遇到了一个问题。
AttributeError: '_MainProcess' object has no attribute '_exiting'
不幸的是,这段代码必须在Python 2.5上运行,因此使用的processing
模块现在被称为multiprocessing
。我正在做的是创建一个Process
(进程),并通过一个Queue
(队列)从主进程中放入一个项目。查看processing.queue
的代码,我发现启动了一个“喂养线程”。这个喂养线程会检查currentProcess()._exiting
,但是currentProcess()
返回的是一个_MainProcess
,而这个主进程没有这个属性,这在processing.process
模块中可以看到。这个问题该怎么解决呢?这是processing
模块的一个bug吗?如果是的话,我可以简单地用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()
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()