首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多处理池PicklingError

多处理池PicklingError
EN

Stack Overflow用户
提问于 2016-08-10 20:38:47
回答 1查看 92关注 0票数 0

我正在处理一个类,它可以接收请求类型对象的列表并执行请求(使用对象的.execute() API)并返回请求的结果。我遇到了一个问题,当我试图使这个多处理。我曾经使用过Pool,现在我很难让它在这里工作。当试图从池中运行Executor类中的staticmethod时,我会收到一个PicklingError。但是,当直接从一个新的流程对象运行时,它似乎像预期的那样工作。造成这种行为的原因是什么?有什么明显的事情我在这里做错了吗?在Python2.6 btw上运行这个

谢谢!

遗嘱执行人类别:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-10 20:46:03

这应该可以解释您拥有的问题:Can't pickle static method - Multiprocessing - Python

顺便提一句:如果我要花大部分时间等待IO,我就不会使用多重处理。尝试线程,甚至协同(http://www.gevent.org/)。你会得到同样的表现,而且不那么小题大做

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38883113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档