50 lines
762 B
Python
Executable File
50 lines
762 B
Python
Executable File
"""
|
|
Multiprocessing
|
|
crear y detener procesos
|
|
compartir datos entre proscesos
|
|
uso de 'lock' para prevenir 'race-conditions'
|
|
uso de 'queue'
|
|
administrar multiples procesos
|
|
"""
|
|
|
|
from multiprocessing import Process, Array, Lock, Queue
|
|
import time
|
|
|
|
def cuadrado(numeros, queue):
|
|
for i in numeros:
|
|
queue.put(i*i)
|
|
|
|
def hacer_negativo(numeros, queue):
|
|
for i in numeros:
|
|
queue.put(-1*i)
|
|
|
|
if __name__ == "__main__":
|
|
|
|
numeros = range(1,6)
|
|
q = Queue()
|
|
|
|
p1 = Process(target=cuadrado, args=(numeros, q))
|
|
p2 = Process(target=hacer_negativo, args=(numeros, q))
|
|
|
|
p1.start()
|
|
p2.start()
|
|
|
|
p1.join()
|
|
p2.join()
|
|
|
|
while not q.empty():
|
|
print(q.get())
|
|
|
|
"""
|
|
1
|
|
4
|
|
9
|
|
16
|
|
25
|
|
-1
|
|
-2
|
|
-3
|
|
-4
|
|
-5
|
|
"""
|