简体   繁体   中英

Multiprocessing in Python: impossible to get back my results (get()) (happens rarely)

I use Multiprocessing in Python in order to do several requests to a database (and other stuff):

po = multiprocessing.Pool()
for element in setOfElements:
    results.append(po.apply_async(myDBRequestModule, (element, other stuff...)))                    
po.close()
po.join()
for r in results:
    newSet.add(r.get())

myDBRequestModule returns an object I defined, made of a list and two numbers. I redefined the hash function, in order to define what I mean by equality in my sets of these objects:

class myObject:
    def __init__(self, aList, aNumber, anotherNumber):
        self.list = aList
        self.number1 = aNumber
        self.number2 = anotherNumber
    def __hash__(self):
        # turn elements of list into a string, in order to hash the string
        hash_text = ""
        for element in self.list:
            hash_text += str(element.x.id) # I use the ID of the element of my list...
        return hash(hash_text)
    def __eq__(self, other):
        self_hash_text = ""
        other_hash_text = ""
        for element in self.list:
            self_hash_text += str(element.x.id)
        for element in other.listDest:
            other_hash_text += str(element.x.id)
        return self_hash_text == other_hash_text 

And in most cases it works as it should. Twice, for no known reason and in exactly the same context, I had a bug:

newSet.add(r.get())
File "/usr/lib/python2.6/multiprocessing/pool.py", line 422, in get
    raise self._value
TypeError: 'str' object does not support item assignment

It comes from the get method (last line):

def get(self, timeout=None):
    self.wait(timeout)
    if not self._ready:
        raise TimeoutError
    if self._success:
        return self._value
    else:
        raise self._value

Since I had this mistake only once and it disappeared, I decided to give up earlier, but it created a second problem recently, and I really don't know how to fight this bug. In particular, it's difficult for me to tell why it happens almost never, and usually works perfectly fine.

multiprocessing is not the issue here.

You have not given us the right code to diagnose the issue. At some point you have assigned a caught exception to self._value . That is where the error is occurring. Look at everywhere that self._value is assigned and you will be on your way to finding this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM