色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:58日期: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 編程
主站蜘蛛池模板: 日韩一区二区三区四区 | 精品亚洲大全 | 国产成人综合95精品视频免费 | 三级网站国产 | 另类二区三四 | 国产91精品一区二区视色 | 亚洲成人网页 | 欧美日韩亚洲一区二区三区 | 免费观看日本特色做爰视频在线 | 亚洲福利精品一区二区三区 | 欧美黄网站免费观看 | 亚洲久久久久久久 | 成人欧美一区二区三区视频xxx | 免费看一级做a爰片久久 | 成人18在线视频播放 | 国产日本在线视频 | 91精品国产高清久久久久久91 | 欧美一区二区三区男人的天堂 | 在线免费一区二区 | 96精品视频在线播放免费观看 | 欧美一区三区 | www.成人网 | 国产精品久久福利网站 | 成年人看的免费视频 | 亚洲国产第一区二区三区 | 免费黄色三级网站 | 欧美成人午夜毛片免费影院 | 欧美午夜免费一级毛片 | 国产免费久久精品99久久 | 久久成人a毛片免费观看网站 | 午夜国产亚洲精品一区 | 三级免费毛片 | xxxxbbbb国产精品 | 本道久久综合88全国最大色 | 久久美女精品国产精品亚洲 | 欧美又粗又硬又大久久久 | 日本一级特黄在线播放 | 中国国产一国产一级毛片视频 | 国产一级一级一级国产片 | 国产精品99久久久久久人 | 中文字幕成人在线观看 |