解决python多线程报错:AttributeError: Can't pickle local object问题


Posted in Python onApril 08, 2020

报错信息:

Traceback (most recent call last):
File “D:/flaskProject/test.py”, line 35, in test
pool.apply(self.out, args=(i,))
File “Python37-32\lib\multiprocessing\pool.py", line 261, in apply
return self.apply_async(func, args, kwds).get()
File "\lib\multiprocessing\pool.py”, line 657, in get
raise self._value
File “\Python37-32\lib\multiprocessing\pool.py", line 431, in _handle_tasks
put(task)
File "\Python37-32\lib\multiprocessing\connection.py”, line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File “*\Python37-32\lib\multiprocessing\reduction.py”, line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: can't pickle _thread._local objects

原类的构造函数:

class threadtest:

def __init__(self, ipList, user, password):
 self.ipList = ipList
 self.httpAuth = HTTPDigestAuth(user, password)
 return

def out(self, i):
 url = "http://" + i + "/name"
 response = requests.get(url, self.httpAuth)
 print(response.text)
 return

def test(self):
 pool = Pool(processes=2)
 for i in self.ipList:
 pool.apply(self.out, args=(i,))
 pool.close()
 pool.join()
 return
if name == ‘main':
ipList = [‘192.168.2.1', ‘192.168.2.2', ‘192.168.2.3', ‘192.168.2.4', ‘192.168.2.5', ]
a = threadtest(ipList, ‘admin', ‘admin')
a.test()

原因:

在class中对属性进行初始化使用了其它类返回的句柄进行初始化导致,HTTPDigestAuth的返回值不能进行序列化,也就是不能作为cls(buf, protocol).dump(obj)的参数进行序列化。

将self.httpAuth = HTTPDigestAuth(httpUser, httpPassword)修改为:

self.httpUser
self.httpPassword

并将函数HTTPDigestAuth放到类的方法中

修改后:

class threadtest:

def __init__(self, ipList, user, password):
 self.ipList = ipList
 self.user = user
 self.password = password
 return

def out(self, i):
 url = "http://" + i + "/name"
 response = requests.get(url, HTTPDigestAuth(self.user, self.password))
 print(response.text)
 return

def test(self):
 pool = Pool(processes=2)
 for i in self.ipList:
 pool.apply(self.out, args=(i,))
 pool.close()
 pool.join()
 return
if name == ‘main':
ipList = [‘192.168.2.1', ‘192.168.2.2', ‘192.168.2.3', ‘192.168.2.4', ‘192.168.2.5', ]
a = threadtest(ipList, ‘admin', ‘admin')
a.test()

以上这篇解决python多线程报错:AttributeError: Can't pickle local object问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python选择排序算法的实现代码
Nov 21 Python
Tensorflow中的placeholder和feed_dict的使用
Jul 09 Python
Python wxpython模块响应鼠标拖动事件操作示例
Aug 23 Python
符合语言习惯的 Python 优雅编程技巧【推荐】
Sep 25 Python
对Python 窗体(tkinter)树状数据(Treeview)详解
Oct 11 Python
python用列表生成式写嵌套循环的方法
Nov 08 Python
Python3实现的简单工资管理系统示例
Mar 12 Python
详解Python 解压缩文件
Apr 09 Python
python画蝴蝶曲线图的实例
Nov 21 Python
python编写一个会算账的脚本的示例代码
Jun 02 Python
Elasticsearch py客户端库安装及使用方法解析
Sep 14 Python
python学习之使用Matplotlib画实时的动态折线图的示例代码
Feb 25 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
Apr 08 #Python
TensorFlow2.1.0安装过程中setuptools、wrapt等相关错误指南
Apr 08 #Python
解决windows下python3使用multiprocessing.Pool出现的问题
Apr 08 #Python
python操作yaml说明
Apr 08 #Python
python 在右键菜单中加入复制目标文件的有效存放路径(单斜杠或者双反斜杠)
Apr 08 #Python
python将音频进行变速的操作方法
Apr 08 #Python
Python读取配置文件(config.ini)以及写入配置文件
Apr 08 #Python
You might like
php使用sql server验证连接数据库的方法
2014/12/25 PHP
php利用递归实现删除文件目录的方法
2016/09/23 PHP
用javascript自动显示最后更新时间
2007/03/15 Javascript
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
判断及设置浏览器全屏模式
2014/04/20 Javascript
node.js中的fs.writeFile方法使用说明
2014/12/14 Javascript
JavaScript实现上下浮动的窗口效果代码
2015/10/12 Javascript
莱鸟介绍window.print()方法
2016/01/06 Javascript
bootstrap模态框垂直居中效果
2016/12/03 Javascript
解析Vue2.0双向绑定实现原理
2017/02/23 Javascript
JS获取鼠标坐标并且根据鼠标位置不同弹出不同内容
2017/06/12 Javascript
get  post jsonp三种数据交互形式实例详解
2017/08/25 Javascript
React Native 真机断点调试+跨域资源加载出错问题的解决方法
2018/01/18 Javascript
jQuery替换节点元素的操作方法
2018/03/18 jQuery
webpack4.0打包优化策略整理小结
2018/03/30 Javascript
Spring boot 和Vue开发中CORS跨域问题解决
2018/09/05 Javascript
详解微信小程序实现仿微信聊天界面(各种细节处理)
2019/02/17 Javascript
vue form表单post请求结合Servlet实现文件上传功能
2021/01/22 Vue.js
[03:56]DOTA2完美大师赛趣味视频之小鸽子和Mineski打台球
2017/11/24 DOTA
全面了解Python的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
详解Python文本操作相关模块
2017/06/22 Python
Python实现的单向循环链表功能示例
2017/11/10 Python
python监控文件并且发送告警邮件
2018/06/21 Python
使用Python将Mysql的查询数据导出到文件的方法
2019/02/25 Python
python输出数学符号实例
2020/05/11 Python
python中format函数如何使用
2020/06/22 Python
利用CSS3的checked伪类实现OL的隐藏显示的方法
2010/12/18 HTML / CSS
使用CSS3来代替JS实现交互
2017/08/10 HTML / CSS
简单英文演讲稿
2014/01/01 职场文书
酒店拾金不昧表扬信
2014/01/18 职场文书
网上开商店的创业计划书
2014/01/19 职场文书
报社实习生自荐信
2014/01/24 职场文书
尊老爱幼演讲稿
2014/09/04 职场文书
文明旅游倡议书
2015/04/28 职场文书
律师函格式范本
2015/05/27 职场文书
redis中lua脚本使用教程
2021/11/01 Redis