I'm trying to use multiprocessing's Pool.map() function to divide out work simultaneously. When I use the following code, it works fine:

我正在尝试使用multiprocessing的Pool.map()函数来同时划分工作。当我使用以下代码时,它工作得很好:

import multiprocessing

def f(x):
    return x*x

def go():
    pool = multiprocessing.Pool(processes=4)        
    print pool.map(f, range(10))


if __name__== '__main__' :
    go()

However, when I use it in a more object-oriented approach, it doesn't work. The error message it gives is:

然而,当我在更面向对象的方法中使用它时,它就不起作用了。它给出的错误信息是:

PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup
__builtin__.instancemethod failed

This occurs when the following is my main program:

这发生在以下是我的主要计划:

import someClass

if __name__== '__main__' :
    sc = someClass.someClass()
    sc.go()

and the following is my someClass class:

下面是我的课程:

import multiprocessing

class someClass(object):
    def __init__(self):
        pass

    def f(self, x):
        return x*x

    def go(self):
        pool = multiprocessing.Pool(processes=4)       
        print pool.map(self.f, range(10))

Anyone know what the problem could be, or an easy way around it?

有人知道问题出在哪里吗?

9 个解决方案

#1


97

The problem is that multiprocessing must pickle things to sling them among processes, and bound methods are not picklable. The workaround (whether you consider it "easy" or not;-) is to add the infrastructure to your program to allow such methods to be pickled, registering it with the copy_reg standard library method.

问题是,多处理必须将事物pickle到进程之间,并且绑定方法是不可pickle的。解决方案(不管你是否认为它“简单”)是将基础结构添加到程序中,以使这些方法被pickle,并使用copy_reg标准库方法注册它。

For example, Steven Bethard's contribution to this thread (towards the end of the thread) shows one perfectly workable approach to allow method pickling/unpickling via copy_reg.

例如,Steven Bethard对这个线程的贡献(接近线程的末尾)显示了一种完全可行的方法,允许通过copy_reg对方法进行pickle / unpickle。

更多相关文章

  1. 在save方法中创建两个对象
  2. 基于Python的XSS测试工具XSStrike使用方法
  3. 关于Python的属性、参数、方法的解释、区别
  4. 【好文收藏】理解python多线程
  5. 集成erlang和python的最佳方法
  6. python中查看变量内存地址的方法
  7. RHEL6误安装RHEL7的包导致glibc被升级后系统崩溃处理方法
  8. Linux--多线程之线程的取消pthread_cancel
  9. Linux多线程——异步

随机推荐

  1. Android 系统监听,监听USB的插拔
  2. android认识
  3. Kotlin初体验(一)-引入Android(安卓)Studio
  4. Android RatingBar案例
  5. Android 开发环境
  6. Android 歌词Lrc显示 自定义View
  7. Android NDK R8 发布
  8. android美女电子相册
  9. android ril 代码逻辑分析
  10. Android开源:数据库ORM框架GreenDao学习(二