I have some functions which may take a longer time to execute, in which case I would like to cut them short (in such a case I do not care about what happens in the function and the consequences to stop it short)
Since these functions are not all mine, I would like to exert this control from the calling program and not implement a check within the function itself (it would be a solution if there is a loop in the function where I could check the time spent, or use timeouts on some calls which support it etc.).
In other words I do not want to change the function.
Is such a mechanism available?
My immediate idea was to start a thread with the function as worker and periodically check if the thread is still live, killing it if it has not came back after the limit time. Unfortunately I learned that killing a thread in a non-cooperative way is not possible (that would be a solution for a process, though but using a process is not practical because it would complexify the existing shared objects).
EDIT: please note that this is not a duplicate of How to limit execution time of a function call in Python, the solutions there either point to a cooperative shutdown, or process vs thread. Both are addressed in my question. I slightly modified the title to emphasize where the control is.