How to break own extension code without killing the interpreter?

I am working on a project that combines high-performance algorithms written in C ++ with the Python interface. C ++ classes and functions are wrapped and exposed to Python using the Cython compiler.

Suppose for the Python interpreter I call a long running native function (my preferred IPython). How can you interrupt or interrupt the execution of this code without killing the interpreter?

+2
source share
3 answers

Here is a possible implementation using multiprocessing , as suggested by Ricardo K.,

 import multiprocessing as mpr def dangerwrap(f): """ I assume f is, or eventually calls, some external function, like cython wrapped C/C++. Also assuming that f returns an object and takes no parameters """ event = mpr.Event() q = mpr.Queue() def signalling_f(): q.put(f()) event.set() f_process = mpr.Process(target = signalling_f) f_process.start() try: event.wait() except KeyboardInterrupt: f_process.terminate() f_process.join() print "Caught in dangerwrap" return None print "Exiting normally" return q.get() 

Now instead of <

 X = f() 

which will not respond to keyboard interruptions, causing

 X = dangerwrap(f) 

will stop with keyboard interruption.

+2
source

Well, I assume that you are trying to run some piece of optimized code that might run into difficulties (for example, working longer than expected), and then you need to kill it.

My understanding is that stopping running code is simply not possible without killing the interpreter, since C / C ++ code will be run from the control of the Python virtual machine. Thus, one option would be to use a standard multiprocessing module to run code in a separate process.

Thus, you can freely transfer data back and forth, and that would add the ability to kill a new process using any standard means, for example. Process.terminate , os.kill ... from the parent process; or any command line / graphical tool that your OS provides.

+1
source

The cysignals package addresses this issue. See this answer .

0
source

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


All Articles