【问题标题】:Simply way to pass variables between processes?在进程之间传递变量的简单方法?
【发布时间】:2020-10-25 18:22:33
【问题描述】:
有个问题我找了好久没找到。
如何在不使用Queue 或Pipe 的情况下在python 进程之间传递变量?
import multiprocessing
string = "hi"
def my_process():
global string
string = "success!" # but its only localy...
multiprocessing.Process(target=my_process).start()
我正在寻找一种方法来更改来自不同脚本或进程的变量...
【问题讨论】:
标签:
python
python-multiprocessing
【解决方案1】:
Guli 即使在它自己的例子上也不起作用
runfile('Z:/Trading/dm_python_bot_mt/discord_bot_main.py', wdir='Z:/Trading/dm_python_bot_mt')
Traceback(最近一次调用最后一次):
文件“”,第 1 行,在
文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\spawn.py”,第 116 行,在 spawn_main
exitcode = _main(fd, parent_sentinel)
文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\spawn.py”,第 125 行,在 _main
准备(preparation_data)
准备中的文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\spawn.py”,第 236 行
_fixup_main_from_path(数据['init_main_from_path'])
_fixup_main_from_path 中的文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\spawn.py”,第 287 行
main_content = runpy.run_path(main_path,
文件“H:\anaconda3\envs\plaid-ml\lib\runpy.py”,第 265 行,在 run_path
return _run_module_code(code, init_globals, run_name,
_run_module_code 中的文件“H:\anaconda3\envs\plaid-ml\lib\runpy.py”,第 97 行
_run_code(代码,mod_globals,init_globals,
_run_code 中的文件“H:\anaconda3\envs\plaid-ml\lib\runpy.py”,第 87 行
执行(代码,run_globals)
文件“Z:\Trading\dm_python_bot_mt\discord_bot_main.py”,第 11 行,在
multiprocessing.Process(target=my_function).start()
文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\process.py”,第 121 行,开始
self._popen = self._Popen(self)
_Popen 中的文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\context.py”,第 224 行
返回 _default_context.get_context().Process._Popen(process_obj)
_Popen 中的文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\context.py”,第 327 行
返回 Popen(process_obj)
init 中的文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\popen_spawn_win32.py”,第 45 行
prep_data = spawn.get_preparation_data(process_obj._name)
文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\spawn.py”,第 154 行,在 get_preparation_data
_check_not_importing_main()
_check_not_importing_main 中的文件“H:\anaconda3\envs\plaid-ml\lib\multiprocessing\spawn.py”,第 134 行
引发 RuntimeError('''
运行时错误:
已尝试在
当前进程已完成其引导阶段。
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
【解决方案2】:
最后,
我为 Python 创建了一个包来解决这个问题。
从 PIP 安装 Guli。
$ pip install guli
Guli 不需要安装任何额外的 PIP 包。
有了这个包就可以了
Guli 可用于在不同的 Python 脚本之间、在多个进程之间或在同一个脚本之间传递。
在主进程和另一个(多进程)进程之间传递变量。
- 在不同的 Python 脚本之间传递变量。
- 在“主进程”和另一个(多进程)进程之间传递变量。
- 在同一脚本中使用变量。
- 创建/删除/编辑 - GuliVariables。
示例
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!!!"
希望我为很多人解决了这个问题!