爲什麼在使用threads
時運行下面的代碼,但在使用時出現異常multiprocessing
被使用?爲什麼multiprocessing.pool.map引發PicklingError(Encoding)?
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadsPool
import urllib2
urls = [
'http://www.python.org',
'http://www.python.org/about/',
'http://www.python.org/doc/',
'http://www.python.org/download/']
def use_threads():
pool = ThreadsPool(4)
results = pool.map(urllib2.urlopen, urls)
pool.close()
pool.join()
print [len(x.read()) for x in results]
def use_procs():
p_pool = Pool(4)
p_results = p_pool.map(urllib2.urlopen, urls)
p_pool.close()
p_pool.join()
print 'using procs instead of threads'
print [len(x.read()) for x in p_results]
if __name__ == '__main__':
use_procs()
唯一的例外是
Traceback (most recent call last):
File "pools.py", line 39, in <module>
use_procs()
File "pools.py", line 31, in use_procs
p_results = p_pool.map(urllib2.urlopen, urls)
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 250, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 554, in get
raise self._value
multiprocessing.pool.MaybeEncodingError: Error sending result: '[<addinfourl at 35286624 whose fp = <socket._fileobject object at 0x2198ad0>>]'. Reason: 'PicklingError("Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed",)'
我知道有間進程和線程如何相互溝通的差異。爲什麼pickle
網站內容失敗?我如何設置編碼來解決這個問題?
那錯誤,因爲你試圖序列套接字對象,這是不可能的 –
有一個想法,我應該通過什麼樣的功能映射到獲得所需的輸出? (讀取對象的執行) – Vinny