I'm wondering, does process in python have its own copy of os.environ
? Ie is it safe to set os.environ
from the process, and is it guaranteed that it will not be overwritten from another process?
import os
import time
import random
from multiprocessing import Process
def f1(i):
time.sleep(random.randint(0,3))
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = str(i)
print('i:', i)
time.sleep(random.randint(0, 3))
print('i:', i, "os.environ['CUDA_VISIBLE_DEVICES']", os.environ['CUDA_VISIBLE_DEVICES'])
def run_me():
n = 3
process_list = []
for i in range(n):
p = Process(target=f1, args=(i,))
p.start()
process_list.append(p)
for p in process_list:
p.join()
if __name__ == "__main__":
run_me()
Example of output:
i: 2
i: 0
i: 0 os.environ['CUDA_VISIBLE_DEVICES'] 0
i: 2 os.environ['CUDA_VISIBLE_DEVICES'] 2
i: 1
i: 1 os.environ['CUDA_VISIBLE_DEVICES'] 1
No process is allowed to modify the enviornment variables of another process, and every process is given its own fresh copy of enviorment variables. It will initally contain a copy of the parent process' env vars unless otherwise specified.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.