Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got an

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

from a Python application. Unfortunately this code has to run Python 2.5 and therefore the processing module nowadays known as multiprocessing. What I was doing is to create a Process with a Queue and to put an item in the queue from the main process. Looking into the processing.queue code I can see that a feeder thread is started. This feeder thread will then check currentProcess()._exiting, but currentProcess() evaluates to a _MainProcess which does not have said attribute as can be seen in the processing.process module. How to solve this? Is it a bug in processing? If yes, can I simply monkeypatch it using currentProcess()._exiting = False?

Minimal example:

#!/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()
like image 885
Helmut Grohne Avatar asked Nov 11 '10 15:11

Helmut Grohne


1 Answers

I am not sure why you would want to pickle a function in this case, if you really want to do that have a look at this answer: Is there an easy way to pickle a python function (or otherwise serialize its code)?

otherwise, this works for python 2.6 (I know you are looking for 2.5 but I don't have 2.5). I have replaced your lambda function with a regular function and provide that to the processing constructor:

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()
like image 76
DrDee Avatar answered Sep 23 '22 19:09

DrDee