如何在 main class 中使用 os 环境变量,它是通过多处理子方法设置的?多处理场景

How to use os environment variable in main class, which set through multiprocessing sub methods? multiprocessing scenario

我正在处理使用多处理的应用程序。

# Main class
# some code...

def executor():
  os.environ['test_count'] = "setting this os. I want to use this os variable in main class"

def run():
  # Some code ..........
  process = Process(target=executor, args=(...))  # executer method gets call from here


  # Some code ..........
  # want to use 'test_count' here somewhere

在执行程序方法中,我设置了一个 os 变量。我想在 main class.

中使用该变量

感谢任何帮助。

您可以使用 multiprocessing 模块中的进程共享 queue。代码可能如下所示:

from multiprocessing import Process, Queue

def executor(queue):
    queue.put({"key": "value"})

def run(queue):
    process = Process(target=executor, args=(queue,))
    process.start()
    process.join()
    print(queue.get())

if __name__ == '__main__':
    run(Queue())

输出:

>>> {'key': 'value'}

还有proxy objects多进程共享数据