Limiting execution time of embedded Python

前端 未结 3 1730
滥情空心
滥情空心 2021-02-19 18:02

If I embed the Python interpreter in a C or C++ program, as in this example, is there any way to limit how long the interpreter runs for? Is there anything to stop the Python co

3条回答
  •  难免孤独
    2021-02-19 18:42

    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.

提交回复
热议问题