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.
Related
I have a multi-threaded C program, which runs perfectly fine as a standalone program. It uses pthread_create and pthread_join to execute some some logic. Now, I am trying to execute this code from python using subprocess. However, when executing via subprocess, it seems subprocess returns as soon as the main thread exits, but I wish to wait for the entire code to finish executing. Is it possible to do this?
I'm writing a Python program that runs under Mac OS and Linux, and I want to run some logic in a multiprocessing.Process. That logic will take a while, and I want it to continue running even after my program is finished and has exited. i.e., I want the main process to not wait for the auxiliary process to finish. I want the main process to exit as soon as it's finished.
I made a few experiments, and it seems that this behavior is the default when using subprocess, but I can't make it happen using multiprocessing.Process, even when I run set_start_method('spawn').
Do you know of a way to get multiprocessing.Process to behave this way?
Looks like starting a new process, and then calling os.fork from it does the trick.
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?
I have an issue with interrupting the subprocess.Popen This is the setup:
I have a Tkinter gui and it is running a another python script using the Popen. This inner script (let's call it running script) is running a shell shell script that is executing a piece of C++ code using a Popen so the hierarchical structure is looking like this:
GUI
\running_script
\shell-script
\c++
running_script works so that if it receives an interrupt, it sends SIGINT to shell-script. If i run a shell_script with my piece of C++ code with the running_script alone and do a CRTL+C everything works like a charm. However if i do it with GUI running the running_script as Popen, SIGINT is sent to the running_script is receives it properly and sends the interrupt signal to shell-script, but instead of terminating the inner process(being c++ code), the shell-script terminates itself and the C++ process continues running, as it was ran in the background, but it was not. When I execute ps -xaf the tree looks like this:
GUI
\running_script
\shell-script <defunct>
c++
So to reiterate, when i run it without the GUI it works like a charm, but with GUI it behaves as explained above. I've tried sending shell-command a SIGTERM instead of SIGINT as well, the result is the same.
You could catch SIGINT in the shell script and make it send SIGINT to the c++ program, e.g:
#!/bin/bash
./cpp_program &
procpid=$!
function killit() {
kill -SIGINT $procpid
}
trap killit SIGINT
......
How do I write to logs, close files etc. when my python program gets killed by a system shutdown? The program is a long running process which uses a while True loop i.e.
while True:
# Open files
#do long running process
If the system is shutdown while this is running, can it get a signal so that it knows to cleanup e.g. close files, write "shutting down..." to log etc. What would the code look like to handle this?
Code is Python 3 running on Debian Wheezy. Program will be started by an init script on system start up.
Thanks.