我正在使用Python中的"spawn"
多进程上下文创建一个新的进程。
import multiprocessing
ctx = multiprocessing.get_context("spawn")
proc = ctx.Process(target=my_func)
proc.start()
我希望该过程具有一组环境变量。理想情况下,我在创建进程时应指定如下:
proc = ctx.Process(target=my_func, environment={"MY_NAME": "MY_VALUE"})
有没有什么方法可以做到这一点?我希望Python运行时这样做,以确保在运行任何Python代码之前设置这些环境变量。
ctx.Process()
之前设置环境变量,要么将一个函数作为target
传递,该函数设置环境,然后调用my_func
。 - Kurtis Rader