right way to run some code with timeout in Python

后端 未结 9 1749
余生分开走
余生分开走 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:11

    For "normal" Python code, that doesn't linger prolongued times in C extensions or I/O waits, you can achieve your goal by setting a trace function with sys.settrace() that aborts the running code when the timeout is reached.

    Whether that is sufficient or not depends on how co-operating or malicious the code you run is. If it's well-behaved, a tracing function is sufficient.

提交回复
热议问题