午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

解決python多線程報錯:AttributeError: Can't pickle local object問題

瀏覽:146日期:2022-07-30 17:16:50

報錯信息:

Traceback (most recent call last):File “D:/flaskProject/test.py”, line 35, in testpool.apply(self.out, args=(i,))File “Python37-32libmultiprocessingpool.py', line 261, in applyreturn self.apply_async(func, args, kwds).get()File 'libmultiprocessingpool.py”, line 657, in getraise self._valueFile “Python37-32libmultiprocessingpool.py', line 431, in _handle_tasksput(task)File 'Python37-32libmultiprocessingconnection.py”, line 206, in sendself._send_bytes(_ForkingPickler.dumps(obj))File “*Python37-32libmultiprocessingreduction.py”, line 51, in dumpscls(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) returndef out(self, i): url = 'http://' + i + '/name' response = requests.get(url, self.httpAuth) print(response.text) returndef 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.httpUserself.httpPassword

并將函數HTTPDigestAuth放到類的方法中

修改后:

class threadtest:def __init__(self, ipList, user, password): self.ipList = ipList self.user = user self.password = password returndef out(self, i): url = 'http://' + i + '/name' response = requests.get(url, HTTPDigestAuth(self.user, self.password)) print(response.text) returndef 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 編程
主站蜘蛛池模板: 成人做受黄大片 | 亚洲综合精品视频 | 麻豆国产在线视频 | 91美女在线 | 色悠久久久 | 91久久精品一区二区三 | 久久免费在线视频 | 奇米影视亚洲春色 | 99久久影院 | 成人女同av免费观看 | 欧美综合视频在线 | 亚洲小视频在线观看 | 欧美xxxx黑人xyx性爽 | 日韩一区二区在线观看视频 | 日韩三区四区 | 欧美资源网 | 精品一区在线 | 色99在线| 亚洲免费天堂 | 九九免费 | 成年人免费看视频 | 一区二区三区蜜桃 | 午夜精品一二三区 | 天天天天天天干 | 91极品视频 | 免费毛片播放 | 欧美亚洲网站 | www四虎| 四虎影院成人 | 国产午夜三级一区二区三 | 亚洲第一页乱 | 日本男人的天堂 | 豆国产97在线 | 亚洲 | 丁香婷婷六月 | 性生交大片免费全黄 | 成人精品一区二区三区中文字幕 | 校园春色综合网 | h片在线免费看 | 日本一区二区三区视频在线观看 | 国产精品久久久精品 | 欧美日韩精品免费 |