Python2.7 PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed 问题解决
# 报错信息
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
# 问题原因
# 根据网上资料就是说: multiprocessing 会对调用的函数进行序列号,而类函数不支持
# 备注: 具体原因待查
# 解决方法 —— 类函数不带返回值
# 1. 在类之外定义调用函数
def run_class_fun(class_name, msg):
class_name.test_func(msg) # 2. 修改 apply_async 调用方式
pool.apply_async(run_class_fun, args=(self, x, ))
# 第一个参数为类外调用函数名
# 第二个参数args的第一个参数为 self, 第二个为类函数参数
# 解决方法 —— 类函数带返回值
# 1. 在类之外定义调用函数
def run_class_fun(class_name, msg):
return class_name.test_func(msg) # 2. 修改 apply_async 调用方式
res.append(pool.apply_async(run_class_fun, args=(self, x, )))
# 第一个参数为类外调用函数名
# 第二个参数args的第一个参数为 self, 第二个为类函数参数
# 全部代码
# -*- coding: utf-8 -*- import multiprocessing
import time class TestClass(object):
def __init__(self):
print('init') def test_func(self, vm_id):
vm_dict = {}
cpu_rate = '22.{}'.format(vm_id)
mem_rate = '33.{}'.format(vm_id)
vm_dict.update({
'vm_id': vm_id,
'cpu_rate': cpu_rate,
'mem_rate': mem_rate
})
return vm_dict def run(self):
res = []
pool = multiprocessing.Pool(processes=10)
for x in range(1, 100):
res.append(pool.apply_async(run_class_fun, args=(self, x, )))
pool.close()
pool.join()
return res def run_class_fun(class_name, msg):
return class_name.test_func(msg) if __name__ == '__main__':
print('----------------START------------------')
t1 = time.time()
# pool = multiprocessing.Pool(processes=10)
# res = []
# for x in range(1, 100):
# res.append(pool.apply_async(run_class_fun, args=(TestClass(), x)))
# pool.close()
# pool.join()
tc = TestClass()
res = tc.run()
print('----------------POOL OVER------------------')
print(time.time()-t1) vm_infos = []
for r in res:
print(r.get())
vm_infos.append(r.get()) print(vm_infos) print('----------------ALL OVER------------------')
print(time.time()-t1)
Python2.7 PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed 问题解决的更多相关文章
- PicklingError: Can't pickle <type 'generator'>: it's not found as __builtin_
多进程传递 参数时,需要是python系统已知的,不然不知道怎么序列化
- python2,socket多进程的错误pickle.PicklingError: Can't pickle
python2,socket多进程的错误pickle.PicklingError: Can't pickle 源码: #coding:utf-8 import socket import pickle ...
- pickle模块 no attribute 'dumps'
今天写了一个pickle.py的文件练习pickle模块,代码如下: import pickle dic = {"linga": ('football',)} dic2 = {&q ...
- chrome下input[type=text]的placeholder不垂直居中的问题解决
http://blog.csdn.net/do_it__/article/details/6789699 <input type="text" placeholder=&qu ...
- springboot jpa mongodb 整合mysql Field in required a bean of type that could not be found Failed to load ApplicationContext
1.完整报错 *************************** APPLICATION FAILED TO START *************************** Descripti ...
- Qt深入:不能不知道的Type、Attribute和Flags
Qter高手与新手的区别在于:知道还是不知道 Qt不是开发语言,所以无所谓谁厉害.但使用他的Qter却有着差异,也许是面向对象语言本身的.或者实际经验上的.而对于Qt本身来说,高手与新手最显著的差异在 ...
- python multiprocess pool模块报错pickling error
问题 之前在调用class内的函数用multiprocessing模块的pool函数进行多线程处理的时候报了以下下错误信息: PicklingError: Can't pickle <type ...
- python 多进程使用总结
python中的多进程主要使用到 multiprocessing 这个库.这个库在使用 multiprocessing.Manager().Queue时会出问题,建议大家升级到高版本python,如2 ...
- python多进程并发进程池Pool
简介: python中的多进程主要使用到 multiprocessing 这个库.低版本python这个库在使用 multiprocessing.Manager().Queue时会出问题,建议大家升级 ...
随机推荐
- [Debian]查看进程、终止进程
# jobs -l [1]+ 115 Running nohup /usr/local/bin/dotnet/dotnet/dotnet /usr/share/nginx/asp/publish/Wi ...
- css引入的方式有哪些_四种css的引入方式与特点
在网页中css主要负责html文档的样式显示,目前css主要有4种引入方式:行内式.内嵌式.导入式.链接式. 1.行内式 行内式也叫内联样式,是指标记的style属性中设定CSS样式,这种方式没有体现 ...
- django 缓存(memcached)
Django提供了6种缓存方式 开发调试缓存 内存缓存 文件缓存 数据库缓存 Memcache缓存(使用python-memcached模块) Memcache缓存(使用pylibmc模块) 常使用的 ...
- day51 表格与表单
目录 一.表格标签 二.表单标签 1 form内最重要的就是input标签,它通过type变形 2 select标签 3 textarea标签 4 重点 三.通过flask实现表单提交 四.css 1 ...
- 本地缓存解决方案-Caffeine Cache
1.1 关于Caffeine Cache Google Guava Cache是一种非常优秀本地缓存解决方案,提供了基于容量,时间和引用的缓存回收方式.基于容量的方式内部实现采用LRU算法,基于引 ...
- JVM 学习笔记(三)
一:使用jvisualvm工具查看堆内存 visualgc插件下载链接 : https://visualvm.github.io/pluginscenters.html --->选择对应版本链接 ...
- 数据可视化之PowerQuery篇(十一)使用Power BI进行动态帕累托分析
https://zhuanlan.zhihu.com/p/57763423 上篇文章介绍了帕累托图的用处以及如何制作一个简单的帕累托图,在 PowerBI 中可以很方便的生成,但若仅止于此,并不足以体 ...
- hihoCoder 1041 国庆出游 最详细的解题报告
题目来源:国庆出游 解题思路(下面是大神的写的): 把题目中的序列称作S,树称作T.那么对于S中的任意节点x,x的子孙节点如果在S出现的话,那么这个子孙节点的位置是有一定要求的:x的所有子孙节点在S中 ...
- 万字长文,62道Java核心面试题,一次性打包送给积极向上的你
先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有一线大厂整理的面试题,以及我的系列文章. ...
- java 正则提取字符串中的电话号码
public static void test2() { String str = "张三:13539558064,李四:15626829748,赵六:13718952204"; ...