Warum unten laufen wird der Code bei der Verwendung von threads
aber eine Ausnahme aus, wenn multiprocessing
verwendet wird?Warum erhöht multiprocessing.pool.map einen 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()
Die Ausnahme ist
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",)'
Ich weiß, es ist der Unterschied zwischen dem, wie Prozesse und Threads miteinander kommunizieren. Warum schlägt der Inhalt der Website pickle
fehl? Wie kann ich die Codierung einstellen, um das zu beheben?
Dass Fehler ausgelöst, weil Sie Socket-Objekt serialisiert werden versuchen, das unmöglich –
Haben Sie eine Idee ist, welche Funktion soll ich zur Karte übergeben Sie die gewünschte Ausgabe zu erhalten? (Lese Ausführung auf dem Objekt) – Vinny