嵌入式python:多重处理不起作用 [英] Embedded python: multiprocessing not working
问题描述
我正在使用作为脚本环境嵌入在应用程序(x64)中的Python 3.1.4. 到目前为止,我在嵌入式python中遇到了很多限制.我不知道这是否正常,或者应用程序的程序员是否阻止了某些功能.
I'm using Python 3.1.4 that is embedded as a scripting environment in an application(x64). So far I have encountered a lot of limitations with the embedded python. I don't know if it is normal or if the programmers of the application have blocked some functionalities.
例如,以下代码不起作用:
For example the following code isn't working:
from multiprocessing import Process
def f(name):
print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
# --> error in forking.py: 'module' object has no attribute 'argv'
# print(sys.argv) gives the same error
sys.executable
返回应用程序的路径.
sys.executable
return the path to the application.
我尝试过这样:
multiprocessing.forking.set_executable('C:\Python31\python.exe')
multiprocessing.set_executable('C:\Python31\python.exe')
没有成功.
是否有解决方法?我不太可能拥有使应用程序开发人员更改其代码中某些内容的能力.
Is there a workaround possible ? It is very unlikely that I would have the leverage to make the developers of the application change something in their code.
谢谢
编辑
我通过添加以下内容使其起作用:
I got it to work by adding the following:
sys.argv = ['c:/pathToScript/scipt.py']
我也需要这一行:
multiprocessing.set_executable('C:/Python31/python.exe')
否则,将打开应用程序的其他实例,而不是运行代码.
Otherwise an other instance of the application open instead of running the code.
我剩下的唯一问题是我无法使用控制应用程序本身的方法(例如:create_project(),add_report()等).我的主要目标是能够调用多个方法,而无需等待第一个方法完成.但是我认为这是不可能的.
The only problem I have left is that I can't use the methods that control the application itself (like: create_project(), add_report(),..). My primary goal was to be able to call multiple methods without the need to wait for the first one to finish completion. But I think this is just not possible.
推荐答案
默认情况下,sys.argv
在嵌入式代码中不可用:
By default, sys.argv
is not available in embedded code:
嵌入Python
基本初始化函数是Py_Initialize().这初始化 加载模块的表,并创建基本模块 Builtins,__ main__和sys.它还初始化模块搜索 路径(sys.path).
Embedding Python
The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and creates the fundamental modules builtins, __main__, and sys. It also initializes the module search path (sys.path).
Py_Initialize()未设置脚本参数列表"(sys.argv).如果 此变量是稍后将要执行的Python代码所需要的,它 必须通过调用PySys_SetArgvEx(argc,argv, 调用Py_Initialize()之后的更新路径)
Py_Initialize() does not set the "script argument list" (sys.argv). If this variable is needed by Python code that will be executed later, it must be set explicitly with a call to PySys_SetArgvEx(argc, argv, updatepath) after the call to Py_Initialize()
在Windows上,multiprocessing
必须从头开始生成新进程.它使用命令行开关--multiprocessing-fork
来区分子进程,还将原始的argv
从父级传输到子级.
On Windows, multiprocessing
must spawn new processes from scratch. It uses a command line switch --multiprocessing-fork
to distinguish child processes, and also transmits the original argv
from parent to child.
在创建子流程之前分配sys.argv = ['c:/pathToScript/scipt.py']
,就像您发现的那样,
似乎是一个不错的解决方法.
Assigning sys.argv = ['c:/pathToScript/scipt.py']
before creating subprocesses, like you discovered,
would seem to be a good workaround.
第二个相关文档是 multiprocessing.set_executable()
:
A second relevant piece of documentation is that of multiprocessing.set_executable()
:
设置Python的路径 启动子进程时使用的解释器. (默认情况下 使用
sys.executable
).嵌入者可能需要做一些事情 喜欢
Sets the path of the Python interpreter to use when starting a child process. (By default
sys.executable
is used). Embedders will probably need to do some thing like
set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
之前
他们可以创建子进程. (仅Windows)
set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
before
they can create child processes. (Windows only)
这篇关于嵌入式python:多重处理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!