I wonder why my program doesn't creates different random number, if I use Pipe
and Process
to create them parallel. My code
import numpy as np
from multiprocessing import Process, Pipe
def createNumbers(conn):
result = np.random.randint(0,2, size=(4))
conn.send(result)
parent_conn, child_conn = Pipe()
for i in range(3):
process = Process(target=createNumbers, args=(child_conn,))
process.start()
process.join()
result = parent_conn.recv()
print(result)
creates three identically vectors
(0, 1, 1, 1)
(0, 1, 1, 1)
(0, 1, 1, 1)
If I use ThreadPool
instead, it works fine. The code
from multiprocessing.pool import ThreadPool
import numpy as np
def createNumbers():
return np.random.randint(0,2, size=4)
for i in range(3):
pool = ThreadPool(processes=1)
process = pool.apply_async(createNumbers)
result = process.get()
print(result)
creates up to three identically vectors
(1, 0, 1, 0)
(0, 0, 1, 0)
(1, 0, 0, 0)
Has someone an idea why the program with Pipe
and Process
creates the same numbers?