I have this code :
import os
pid = os.fork()
if pid == 0:
os.environ['HOME'] = "rep1"
external_function()
else:
os.environ['HOME'] = "rep2"
external_function()
and this code :
from multiprocessing import Process, Pipe
def f(conn):
os.environ['HOME'] = "rep1"
external_function()
conn.send(some_data)
conn.close()
if __name__ == '__main__':
os.environ['HOME'] = "rep2"
external_function()
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print parent_conn.recv()
p.join()
The external_function
initializes an external programs by creating the necessary sub-directories in the directory found in the environment variable HOME
. This function does this work only once in each process.
With the first example, which uses os.fork()
, the directories are created as expected. But with second example, which uses multiprocessing
, only the directories in rep2
get created.
Why isn't the second example creating directories in both rep1
and rep2
?