Timeout on a function call

后端 未结 18 1480
挽巷
挽巷 2020-11-21 04:53

I\'m calling a function in Python which I know may stall and force me to restart the script.

How do I call the function or what do I wrap it in so that if it takes

18条回答
  •  野性不改
    2020-11-21 05:04

    timeout-decorator don't work on windows system as , windows didn't support signal well.

    If you use timeout-decorator in windows system you will get the following

    AttributeError: module 'signal' has no attribute 'SIGALRM'
    

    Some suggested to use use_signals=False but didn't worked for me.

    Author @bitranox created the following package:

    pip install https://github.com/bitranox/wrapt-timeout-decorator/archive/master.zip
    

    Code Sample:

    import time
    from wrapt_timeout_decorator import *
    
    @timeout(5)
    def mytest(message):
        print(message)
        for i in range(1,10):
            time.sleep(1)
            print('{} seconds have passed'.format(i))
    
    def main():
        mytest('starting')
    
    
    if __name__ == '__main__':
        main()
    

    Gives the following exception:

    TimeoutError: Function mytest timed out after 5 seconds
    

提交回复
热议问题