我正在处理一个类,它可以接收请求类型对象的列表并执行请求(使用对象的.execute() API)并返回请求的结果。我遇到了一个问题,当我试图使这个多处理。我曾经使用过Pool,现在我很难让它在这里工作。当试图从池中运行Executor类中的staticmethod时,我会收到一个PicklingError。但是,当直接从一个新的流程对象运行时,它似乎像预期的那样工作。造成这种行为的原因是什么?有什么明显的事情我在这里做错了吗?在Python2.6 btw上运行这个
谢谢!
遗嘱执行人类别:
class Executor(object):
def __init__(self, max_threads):
self.max_threads = max_threads
@staticmethod
def execute_request_partition(requests):
return [request.execute() for request in requests]
def execute_requests(self, list_of_requests):
partitions = split_list(list_of_requests, self.max_threads)
# Seems to execute as expected
process = Process(target=self.execute_request_partition, args=(partitions[0],))
process.run()
# PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
p = Pool(1)
p.apply_async(self.execute_request_partition, args=(partitions[0],))
p.close()
p.join()发布于 2016-08-10 20:46:03
这应该可以解释您拥有的问题:Can't pickle static method - Multiprocessing - Python
顺便提一句:如果我要花大部分时间等待IO,我就不会使用多重处理。尝试线程,甚至协同(http://www.gevent.org/)。你会得到同样的表现,而且不那么小题大做
https://stackoverflow.com/questions/38883113
复制相似问题