How to embed multiple python interpreters in a c++ program with pybind11? - python

Using pybind11, I'd like to embed multiple interpreters: I want to launch multiple threads, and in each thread, start an interpreter, eval_file, and then stop the interpreter and shut down the thread.
Basically, this is a thread function:
pybind11::scoped_interpreter guard{};
pybind11::eval_file(python_filename);
Compilation is fine, but at runtime I get:
terminate called after throwing an instance of 'std::runtime_error'
what(): The interpreter is already running
I guess this is a no-no. But why? How would it even know?

Related

Any way to pause only one thread while debugging Python in vscode?

When I debug a multi-threaded Python program using vscode, I found all threads were suspended when a breakpoint is hitted. Is there any way to choose to suspend only one thread or all like what Pycharm does?

Multiprocessing freezing because of semaphore_tracker process in the background

I am using Python Multiprocessing for a project and sometimes the process freezes and apparently the reason why it is happening is this process I find running ps aux:
python -c from multiprocessing.semaphore_tracker import main;main(39)
Some more info:
If I kill the process everything runs fine
This problem is not frequent, meaning there could be days running everything fine without it happenning
I am using PyCharm
I am runing this Python code in a server using PyCharm remote interpreter and sometime using SSH
Questions:
What is happening that this process is appearing?
Why isn't it finishing by itself?
What does it do that freezes other processes?
How to avoid this situation?
According to the documentation:
On Unix using the spawn or forkserver start methods will also start a semaphore tracker process which tracks the unlinked named semaphores created by processes of the program.
Why one would want to use the spawn start method escapes me. It is a (very clever) bodge necessary on ms-windows because that OS doesn't have the fork system call.
So I suspect that Pycharm imposes the use of the forkserver start method because it uses multiple threads internally, and the standard UNIX fork startmethod doesn't deal well with multithreaded programs.
Try running your project from a shell. On UNIX-like operating systems that should default to the fork start method that does not require the semaphore tracker process.

SIGTERM sent to C Thread in Python Extension Causes Python

I am writing a Python C extension that contains multiple C pthreads. Eventually these threads are sent a SIGTERM in order for them to exit. When I step through the extension in GDB these threads exit successfully, and I return back to the Python interpreter where I can continue to run commands. It is also working successfully in the Python interpreter.
However, when I try to run a Python file that contains similar behavior, the entire program terminates after the signal is sent to the child thread.
I am confused as to how the signal is propagating up from the threads to the program itself, any guidance is appreciated.

CPU occupation while using python subprocess module

I wrote a c++ program a.out and tried to run the program by using subprocess module in python, called run.py. After running the script, I used top to check cpu usage and found that the run.py does not shown in the list while a.out is running.
What happened to run.py? Where is it?
It is sleeping waiting for your C++ program to exit. That is because you used subprocess.call.
You can use subprocess.Popen which doesn't wait unless you call its wait method if you need your Python program to continue doing something else while the program runs.

Python thread that monitors a script

I need to create a python script that monitors another python script, specifically, some variables from it. I was thinking of creating an independent thread in the "observer script" that does this. This thread must run until the "executer script" finishes. Maybe all of this can be done inside one big script (observer+executer), I really do not know at the moment.
Is it possible at all to do this with python in windows?

Categories

Resources