Sto cercando di implementare questo multiprocessing tutorial per i miei scopi. All'inizio ho pensato che non fosse scalabile, ma quando ho fatto un esempio riproducibile ho scoperto che se l'elenco degli oggetti supera 124, sembra non restituire mai una risposta. A x = 124
funziona in .4 secondi, ma quando lo imposto su x = 125
non finisce mai. Sto eseguendo Python 2.7 su Windows 7.Elaborazione multipla Python> = 125 lista non finisce mai
from multiprocessing import Lock, Process, Queue, current_process
import time
class Testclass(object):
def __init__(self, x):
self.x = x
def toyfunction(testclass):
testclass.product = testclass.x * testclass.x
return testclass
def worker(work_queue, done_queue):
try:
for testclass in iter(work_queue.get, 'STOP'):
print(testclass.counter)
newtestclass = toyfunction(testclass)
done_queue.put(newtestclass)
except:
print('error')
return True
def main(x):
counter = 1
database = []
while counter <= x:
database.append(Testclass(10))
counter += 1
print(counter)
workers = 8
work_queue = Queue()
done_queue = Queue()
processes = []
start = time.clock()
counter = 1
for testclass in database:
testclass.counter = counter
work_queue.put(testclass)
counter += 1
print(counter)
print('items loaded')
for w in range(workers):
p = Process(target=worker, args=(work_queue, done_queue))
p.start()
processes.append(p)
work_queue.put('STOP')
for p in processes:
p.join()
done_queue.put('STOP')
newdatabase = []
for testclass in iter(done_queue.get, 'STOP'):
newdatabase.append(testclass)
print(time.clock()-start)
print("Done")
return(newdatabase)
if __name__ == '__main__':
database = main(124)
database2 = main(125)
Funziona per me. – Veedrac
ha pubblicato un'altra domanda qui che ha funzionato per me. http://stackoverflow.com/questions/19070638/python-multiprocessing-ioerror-errno-232-the-pipe-is-being-closed#comment28188856_19070638 –
Ho risolto il problema nell'altro post. Ho provato a eseguire il codice sopra su entrambi i miei computer e su entrambi si blocca alla fine quando 'x = 125' e non stampa mai Fatto. – Michael