I tried to work with timeout decorator(using signal) to timeout a function,But it results in main thread error
Any other solution to timeout a function after some seconds
Thanks
I tried to work with timeout decorator(using signal) to timeout a function,But it results in main thread error
Any other solution to timeout a function after some seconds
Thanks
For me such a problem can be solved using a multithreaded approach as it is not possible for the thread executing your method to preempt the the task running or using an OS functionality to preempt the function running and execute some code.
you could spawn a thread to execute your method code and have the main thread counting down untill the imparted time ellapsed.
Another approach is to use the signal python framework that is capable to send signals mapped to system processes signals (all signals cannot be mapped as not all OSs support them). When such a signal is sent (signal.alarm(3) sends it after 3 seconds) the python process finishes executing the current interpretor instruction and executes the handler you defined.
BTW I have just noticed that your question is a duplicate of this one where you can find a snippet for the second solution.