right way to run some code with timeout in Python

后端 未结 9 1717
余生分开走
余生分开走 2020-12-13 18:18

I looked online and found some SO discussing and ActiveState recipes for running some code with a timeout. It looks there are some common approaches:

  • Use threa
9条回答
  •  [愿得一人]
    2020-12-13 19:21

    An other way is to use faulthandler:

    import time
    import faulthandler
    
    
    faulthandler.enable()
    
    
    try:
        faulthandler.dump_tracebacks_later(3)
        time.sleep(10)
    finally:
        faulthandler.cancel_dump_tracebacks_later()
    

    N.B: The faulthandler module is part of stdlib in python3.3.

提交回复
热议问题