Multiprocesos
Publicado por Quarkbite (1 intervención) el 28/07/2011 22:29:51
querria saber si es posible partiendo del codigo que pongo a continuacion, limitar el numero de procesos abiertos por ejemplo a 5.
Es decir que me pare el for hasta que se libere algun proceso.
CODIGO:
from multiprocessing import Process,Lock,current_process
def f(l, i):
l.acquire()
print current_process()
print 'hello world', i
l.release()
if __name__ == '__main__':
lock = Lock()
for num in range(100):
Process(target=f,args=(lock, num)).start()
SALIDA:
<Process(Process-1, started)>
hello world 0
<Process(Process-5, started)>
hello world 4
<Process(Process-3, started)>
hello world 2
<Process(Process-7, started)>
hello world 6
<Process(Process-2, started)>
hello world 1
<Process(Process-4, started)>
hello world 3
<Process(Process-9, started)>
hello world 8
<Process(Process-6, started)>
hello world 5
<Process(Process-11, started)>
hello world 10
<Process(Process-8, started)>
hello world 7
<Process(Process-13, started)>
hello world 12
<Process(Process-10, started)>
hello world 9
<Process(Process-14, started)>
hello world 13
Es decir que me pare el for hasta que se libere algun proceso.
CODIGO:
from multiprocessing import Process,Lock,current_process
def f(l, i):
l.acquire()
print current_process()
print 'hello world', i
l.release()
if __name__ == '__main__':
lock = Lock()
for num in range(100):
Process(target=f,args=(lock, num)).start()
SALIDA:
<Process(Process-1, started)>
hello world 0
<Process(Process-5, started)>
hello world 4
<Process(Process-3, started)>
hello world 2
<Process(Process-7, started)>
hello world 6
<Process(Process-2, started)>
hello world 1
<Process(Process-4, started)>
hello world 3
<Process(Process-9, started)>
hello world 8
<Process(Process-6, started)>
hello world 5
<Process(Process-11, started)>
hello world 10
<Process(Process-8, started)>
hello world 7
<Process(Process-13, started)>
hello world 12
<Process(Process-10, started)>
hello world 9
<Process(Process-14, started)>
hello world 13
Valora esta pregunta


0