Search code examples
pythonmultithreadingtiming

python, limmiting function running time


I have a function that depends on the internet and downloads files, however sometimes the download time is too long or the sending request processes is failing.I wanted to know how can I set some-thing like a timer for the function, and if it exceeds the time (lets say a minute) to rerun the function from the beginning.


Solution

  • Use signal for time out cases

    First set the signal handler with signal.signal(signal.SIGALRM, handler)

    To set the time for the signal alarm use signal.alarm(seconds)

    If the request to download is taking too long, i.e more than the seconds specified in alarm, the handler will be called upon

    You could check out the documentation.