有件事我找了很长时间想解决却找不到。
如何在python进程之间传递变量,而不使用queue
或pipe
?
import multiprocessing
string = "hi"
def my_process():
global string
string = "success!" # but its only localy...
multiprocessing.Thread(target=my_process).start()
我正在寻找一种从不同的脚本或进程中更改变量的方法。
最后,
我为Python创建了一个包来解决这个问题。
$ pip install guli
Guli不需要安装任何额外的PIP包。
Guli可用于在不同的Python脚本之间,在许多进程之间或在同一脚本处传递。 在主进程和另一个(多进程)进程之间传递变量。
Guli是开源的,在GitHub上有一个公共存储库。
import guli
import multiprocessing
string = guli.GuliVariable("hello").get()
print(string) # returns empty string ""
def my_function():
''' change the value from another process '''
guli.GuliVariable("hello").setValue(4)
multiprocessing.Process(target=my_function).start()
import time
time.sleep(0.01) # delay after process to catch the update
string = guli.GuliVariable("hello").get()
print(string) # returns "success!!!"
Itai Gu写的包裹
希望我为很多人解决了这个问题!