我在 Python 中使用 Multiprocessing 来对数据库(和其他东西)执行多个请求:
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 返回一个我定义的对象,由一个列表和两个数字组成。我重新定义了哈希函数,以便在我的这些对象集中定义我所说的相等性:
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
在大多数情况下,它可以正常工作。两次,出于未知原因,在完全相同的上下文中,我遇到了一个错误:
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
它来自 get 方法(最后一行):
def get(self, timeout=None):
self.wait(timeout)
if not self._ready:
raise TimeoutError
if self._success:
return self._value
else:
raise self._value
由于我只出现过一次这个错误,它就消失了,我决定早点放弃,但最近又出现了第二个问题,我真的不知道如何解决这个错误。特别是,我很难说出为什么它几乎从未发生过,而且通常工作得很好。