joedborg
joedborg

Reputation: 18353

Python kill hanging function

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! And 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.

Upvotes: 1

Views: 1116

Answers (1)

user
user

Reputation: 18599

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

Upvotes: 1

Related Questions