Kill the suspension function in Python in a multi-threaded environment

I would like to kill a function that runs to the end. It is important that this function is inside the C extension (terminated in Cython), and I would like this solution to work in a multi-threaded environment. Since it is wrapped in Cython, this thread may contain a GIL.

I have no control over what is going on inside this extension (and I think this code will not respond to interrupts).

I am sure that this code will only work on Unix machines. But the question The Python freeze function is not applied, because I think that the signals will not work in a multi-threaded environment (AFAIK is undefined, which thread will catch them) --- but I can be wrong about that :) so correct me.

Is there any way to resolve this without the advent of new processes.

+4
source share
1 answer

My solution is to wrap this function in another python process and kill this process if necessary.

I’ll give advice to anyone who asks this question: starting from the moment the process starts (the launching interpreter, loading the modules, and then loading the data into memory), it can be enough for a couple of seconds to group function calls so that this overhead costs; you (therefore, there really is no reusable solution).

An example solution was sent to another question: How to interrupt my own extension code without killing the interpreter? .

+1
source

Source: https://habr.com/ru/post/906125/


All Articles