Built-in python: multiprocessor not working

I am using Python 3.1.4, which is built into the scripting environment in an application (x64). So far, I have encountered many limitations with python built-in. I do not know if this is normal or if application programmers have blocked some functions.

For example, the following code does not work:

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 return the path to the application.

I tried this as well:

 multiprocessing.forking.set_executable('C:\Python31\python.exe') multiprocessing.set_executable('C:\Python31\python.exe') 

Without success.

Is a workaround possible? It is very unlikely that I will have leverage so that application developers can change something in their code.

thanks

EDIT

I got it to work by adding the following:

sys.argv = ['c:/pathToScript/scipt.py']

I also need this line:

multiprocessing.set_executable('C:/Python31/python.exe')

Otherwise, instead of running the code, another instance of the application opens.

The only problem I encountered is that I cannot use the methods that control the application itself (for example: create_project (), add_report (), ..). My main goal was to be able to call multiple methods without having to wait for the first completion to complete. But I think this is simply not possible.

+7
source share
1 answer

By default, sys.argv not available in embedded code:

Python Deployment

The main initialization function is Py_Initialize (). This initializes the table of loaded modules and creates the main modules built-in functions, __main__ and sys. It also initializes the search for the path module (sys.path).

Py_Initialize () does not set the argument list <script "(sys.argv). If this variable is required for Python code to be executed later, it must be set explicitly by calling PySys_SetArgvEx (argc, argv, updatepath) after calling Py_Initialize ()

In Windows multiprocessing , new processes should appear from scratch. It uses the --multiprocessing-fork command line --multiprocessing-fork to allocate child processes, and also passes the original argv from parent to child.

Assigning sys.argv = ['c:/pathToScript/scipt.py'] before creating the subprocesses, as you discovered, would be a good solution.

The second relevant part of the documentation is multiprocessing.set_executable() :

Sets the path to the Python interpreter that will be used when starting the child process. (The default is sys.executable ). Investments probably have to do something like

set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe')) before they can create child processes. (Windows only)

+7
source

All Articles