Многопроцессорность Pybind11 зависает

Я пишу приложение, которое использует Pybind11 для встраивания интерпретатора Python (Windows, 64-разрядная версия, Visual C++ 2017). Из Python мне нужно создать несколько процессов, но, похоже, это не работает. Я пробую следующий код в качестве теста:

import multiprocessing
import os
import sys
import time
print("This is the name of the script: ", sys.argv[0])
print("Number of arguments: ", len(sys.argv))
print("The arguments are: " , str(sys.argv))
prefix=str(os.getpid())+"-"
if len(sys.argv) > 1:
    __name__ = "__mp_main__"

def print_cube(num):
    """
    function to print cube of given num
    """
    print("Cube: {}".format(num * num * num))

def print_square(num):
    """
    function to print square of given num
    """
    print("Square: {}".format(num * num))

print(__name__)

if __name__ == "__main__":
    print(prefix, "checkpoint 1")
    # creating processes
    p1 = multiprocessing.Process(target=print_square, args=(10, ))
    p1.daemon = True
    p2 = multiprocessing.Process(target=print_cube, args=(10, ))

    # starting process 1
    p1.start()
    print(prefix, "checkpoint 2")

    # starting process 2
    p2.start()
    print(prefix, "checkpoint 3")

    # wait until process 1 is finished
    print(prefix, "checkpoint 4")
    p1.join()
    print(prefix, "checkpoint 5")
    # wait until process 2 is finished
    p2.join()
    print(prefix, "checkpoint 6")

    # both processes finished
    print("Done!")
print(prefix, "checkpoint 7")
time.sleep(10)

Запустив его с помощью Python из командной строки, я получаю:

This is the name of the script:  mp.py
Number of arguments:  1
The arguments are:  ['mp.py']
__main__
12872- checkpoint 1
12872- checkpoint 2
This is the name of the script:  C:\tmp\mp.py
Number of arguments:  1
The arguments are:  ['C:\\tmp\\mp.py']
__mp_main__
7744- checkpoint 7
Square: 100
12872- checkpoint 3
12872- checkpoint 4
12872- checkpoint 5
This is the name of the script:  C:\tmp\mp.py
Number of arguments:  1
The arguments are:  ['C:\\tmp\\mp.py']
__mp_main__
15020- checkpoint 7
Cube: 1000
12872- checkpoint 6
Done!
12872- checkpoint 7

что правильно. Если я попробую то же самое из проекта C++ с Pybind11, результат будет таким:

This is the name of the script:  C:\AGPX\Documenti\TestPyBind\x64\Debug\TestPyBind.exe
Number of arguments:  1
The arguments are:  ['C:\\AGPX\\Documenti\\TestPyBind\\x64\\Debug\\TestPyBind.exe']
__main__
4440- checkpoint 1
This is the name of the script:  C:\AGPX\Documenti\TestPyBind\x64\Debug\TestPyBind.exe
Number of arguments:  4
The arguments are:  ['C:\\AGPX\\Documenti\\TestPyBind\\x64\\Debug\\TestPyBind.exe', '-c', 'from multiprocessing.spawn import spawn_main; spawn_main(parent_pid=4440, pipe_handle=128)', '--multiprocessing-fork']
__mp_main__
10176- checkpoint 7

Обратите внимание, что в этом случае переменная __name__ всегда имеет значение «__main__», поэтому мне нужно изменить ее вручную (для порожденных процессов) на «__mp_main__» (я могу обнаружить дочерние процессы благодаря sys.argv). Это первое странное поведение. Родительский процесс имеет pid 4440, и я вижу процесс в проводнике процессов. Первый дочерний процесс имеет pid 10176, он достигает конца «контрольной точки 7», и процесс исчезает из проводника процессов. Однако основной процесс не печатает «контрольную точку 2», то есть похоже, что он зависает на «p1.start()», и я не могу понять, почему. Полный код C++:

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/embed.h>
#include <iostream>

namespace py = pybind11;
using namespace py::literals;

int wmain(int argc, wchar_t **argv)
{
    py::initialize_interpreter();
    PySys_SetArgv(argc, argv);

    std::string pyCode = std::string(R"(
import multiprocessing
import os
import sys
import time
print("This is the name of the script: ", sys.argv[0])
print("Number of arguments: ", len(sys.argv))
print("The arguments are: " , str(sys.argv))
prefix=str(os.getpid())+"-"
if len(sys.argv) > 1:
    __name__ = "__mp_main__"

def print_cube(num):
    """
    function to print cube of given num
    """
    print("Cube: {}".format(num * num * num))

def print_square(num):
    """
    function to print square of given num
    """
    print("Square: {}".format(num * num))

print(__name__)

if __name__ == "__main__":
    print(prefix, "checkpoint 1")
    # creating processes
    p1 = multiprocessing.Process(target=print_square, args=(10, ))
    p1.daemon = True
    p2 = multiprocessing.Process(target=print_cube, args=(10, ))

    # starting process 1
    p1.start()
    print(prefix, "checkpoint 2")

    # starting process 2
    p2.start()
    print(prefix, "checkpoint 3")

    # wait until process 1 is finished
    print(prefix, "checkpoint 4")
    p1.join()
    print(prefix, "checkpoint 5")
    # wait until process 2 is finished
    p2.join()
    print(prefix, "checkpoint 6")

    # both processes finished
    print("Done!")
print(prefix, "checkpoint 7")
time.sleep(10)
)");
    try
    {
        py::exec(pyCode);
    } catch (const std::exception &e) {
        std::cout << e.what();
    }
    py::finalize_interpreter();
}

Может ли кто-нибудь объяснить мне, как преодолеть эту проблему, пожалуйста?

Заранее спасибо (и я извиняюсь за мой английский).


person AGPX    schedule 16.01.2020    source источник


Ответы (1)


Хорошо, благодаря этой ссылке: https://blender.stackexchange.com/questions/8530/how-to-get-python-multiprocessing-module-working-on-windows, я решил эту странную проблему (которая, похоже, связана с Windows). Это не проблема Pybind11, а сам Python C API. Вы можете решить эту проблему, установив sys.executable равным пути к исполняемому файлу интерпретатора Python (python.exe) и записав код Python в файл и установив путь к переменной __file__. То есть я должен добавить:

import sys
sys.executable = "C:\\Users\\MyUserName\\Miniconda3\\python.exe"
__file__ = "C:\\tmp\\run.py"

и мне нужно написать код Python в файл, указанный __file__, то есть:

FILE *f = nullptr;
fopen_s(&f, "c:\\tmp\\run.py", "wt");
fprintf(f, "%s", pyCode.c_str());
fclose(f);

непосредственно перед выполнением py::exec(pyCode).

В дополнение код:

if len(sys.argv) > 1:
    __name__ = "__mp_main__"

больше не нужно. Однако обратите внимание, что таким образом запущенные процессы больше не встраиваются и, к сожалению, если вы хотите напрямую передать им модуль C++, вы не можете этого сделать.

Надеюсь, это может помочь кому-то другому.

person AGPX    schedule 17.01.2020