File size: 2,324 Bytes
4cef980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-

"""
Definition of timeout decorators.

If the clock time of a function/method call, decorated with @timeout,
exceeds a given limit, a handler will be called and the call will be
terminated.
The timeout decorator currently only works in Unix systems.

Copyright (C) 2013 Balthazar Rouberol - <brouberol@imap.cc>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import signal


class Timeout(Exception):
    pass


def timeout_handler(signum, frame):
    raise Timeout


def timeout(limit, handler):
    """A decorator ensuring that the decorated function tun time does not
    exceeds the argument limit.

    :args limit: the time limit
    :type limit: int

    :args handler: the handler function called when the decorated
    function times out.
    :type handler: callable

    Example:
    >>>def timeout_handler(limit, f, *args, **kwargs):
    ...     print "{func} call timed out after {lim}s.".format(
    ...         func=f.__name__, lim=limit)
    ...
    >>>@timeout(limit=5, handler=timeout_handler)
    ... def work(foo, bar, baz="spam")
    ...     time.sleep(10)
    >>>work("foo", "bar", "baz")
    # time passes...
    work call timed out after 5s.
    >>>


    """
    def wrapper(f):
        def wrapped_f(*args, **kwargs):
            old_handler = signal.getsignal(signal.SIGALRM)
            signal.signal(signal.SIGALRM, timeout_handler)
            signal.alarm(limit)
            try:
                res = f(*args, **kwargs)
            except Timeout:
                handler(limit, f, args, kwargs)
            else:
                return res
            finally:
                signal.signal(signal.SIGALRM, old_handler)
                signal.alarm(0)
        return wrapped_f
    return wrapper