请教大家一个 pybind11 的问题, 我的主程序是 c++写的, 将 python 作为脚本调用, 目前有个问题是加载 python 脚本之后, python 里面的线程就停止了, 代码如下:
#include <thread>
#include <pybind11.h>
const char* code = R"(
import sys, os, time, threading
print("hello")
def run():
while True:
print("abc")
time.sleep(1)
t = threading.Thread(target=run)
t.start()
)";
void main(int args, char* argv[])
{
pybind11::scoped_interpreter scoped(false);
pybind11::exec(code);
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}
上面的代码只会输出 1 个 abc. 有什么办法能够让 python 的线程持续运行呢?