亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:24日期: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 編程
主站蜘蛛池模板: 成人毛片一区二区三区 | 国产免费播放一区二区 | 99久久精品无码一区二区毛片 | 一级黄色片免费 | 久久国产精品自线拍免费 | 最新黄色网址在线观看 | 特级aa毛片在线播放 | 一二三区在线视频 | 成人淫片免费视频95视频 | 国语自产自拍秒拍在线视频 | 亚洲福利视频精选在线视频 | 亚洲精品国产一区二区三 | 国产精品自在欧美一区 | 日韩黄色一级视频 | 嗯啊在线观看免费影院 | 国产午夜毛片一区二区三区 | 免费中文字幕不卡视频 | 国产成人精品在视频 | 91久久精品一区二区三区 | 久久精品亚洲精品国产欧美 | 欧美最刺激好看的一级毛片 | 免费在线观看a级毛片 | 成人国产永久福利看片 | 日本一级特黄aa大片在线观看 | 窝窝午夜精品一区二区 | 久久91精品国产91久久跳舞 | 欧美大片欧美毛片大片 | 黄色网免费| 久久久精彩视频 | 91视频播放| 久久99精品视频在线在线观看 | 青青青国产免费手机视频在线观看 | 污污网站大全 | 日韩 第一页 | 久久久国产一区二区三区 | 久草在线视频福利 | 午夜香蕉| 免费看色片网站 | 成年女人毛片免费观看不卡 | 特黄特色大片免费播放 | 中国一级特黄大片毛片 |