如何从另一个Python脚本中生成主流程的子流程?

时间:2018-11-12 16:27:37

标签: python windows subprocess shared-memory python-multiprocessing

我想将以下代码分成两个python脚本。负责更新myVar的人。另一个接收更新的myVar

import multiprocessing

def func(myVar):
    myVar = 2


if __name__ == "__main__":

      myVar = multiprocessing.Value('d',2) #update myVar from the child process

      p = multiprocessing.Process(target = func, args = (myVar,))

      p.start()
      p.join()

      print(myVar) #get the updated myVar

所需结构为: update_myVar.py

def func(myVar):
    myVar = 2


#if __name__ == "__main__": #spawn child process for main.py's main()

      myVar = multiprocessing.Value('d',2) #update myVar from the child process

      p = multiprocessing.Process(target = func, args = (myVar,))

      p.start()
      p.join()

main.py

if __name__ == "__main__":
    print(myVar)

谢谢!

0 个答案:

没有答案