Limiting execution time of embedded Python

As you can see in the docs, PyObject_CallObject has no mechanism for limiting how long the function runs. There is also no Python C API function that I am aware of that allows you to pause or kill a thread used by the interpreter.

We therefore have to be a little more creative in how we stop a thread running. I can think of 3 ways you could do it (from safest/cleanest to most dangerous...

Poll your main application

The idea here is that your Python function which could run for a long time simply calls another function inside your main application, using the C API to see if it should shut down. A simple True/False result would allow you to terminate gracefully.

This is the safest solution, but requires that you alter your Python code.

Use exceptions

Since you are embedding the Interpreter, you are already using the C API and so could use PyThreadState_SetAsyncExc to force an exception to be raised in the offending thread. You can find an example that uses this API here. While it is Python code, the same function will work from your main application.

This solution is a little less safe as it requires the code not to catch the Exception and to remain in a usable state afterwards.

Use the OS to terminate the thread

I'm not going to go into this one as it is inherently unsafe. See Is there any way to kill a Thread in Python? for some explanations of why.


If You need control lag and jitter in some realtime system, embed "StacklessPython" - Python with support co-routines. Used in "Twisted Web Server".