开发者

Python kill hanging function

开发者 https://www.devze.com 2023-03-15 09:16 出处:网络
I\'m sure this has been asked before, but I can\'t find a similar example.I need to be able to call a function with an execution time-limit.If the function takes over this time, then the whole script

I'm sure this has been asked before, but I can't find a similar example. I need to be able to call a function with an execution time-limit. If the function takes over this time, then the whole script exits.

This can't be done by timing the whole script! A开发者_JAVA百科nd I only want one, small function to be timed. Is it possible?

For example

    def sleep():
        time.sleep(60)

time("sleep()", limit=30, violation="sys.exit(1)")

Very basic and horrid looking example I know, but it might help the explanation.


Here's a recipe (unix only) http://code.activestate.com/recipes/534115-function-timeout/

0

精彩评论

暂无评论...
验证码 换一张
取 消