File: resourcelimits.py

package info (click to toggle)
python-bumps 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,200 kB
  • sloc: python: 24,517; xml: 493; ansic: 373; makefile: 211; javascript: 99; sh: 94
file content (53 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download | duplicates (2)
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
"""
Resource limit handling.
"""

try:
    from signal import signal, SIGXCPU, SIG_IGN
    from resource import setrlimit, RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA
except:
    print("limits not supported")
    RLIMIT_CPU = RLIMIT_FSIZE = RLIMIT_DATA = 0
    SIGXCPU = SIG_IGN = 0

    def setrlimit(resource, limits):
        pass

    def signal(value, handler):
        pass


class CPULimit(Exception):
    pass


def _xcpuhandler(signum, frame):
    signal(SIGXCPU, SIG_IGN)
    raise CPULimit("CPU time exceeded.")


def cpu_limit(limit=3600 * 2, recovery_time=120):
    """
    Set limit on the amount of CPU time available to the process.

    Raises resourcelimits.CPULimit when the cpu limit is exceeded, after
    which the program has the number of seconds of recovery time left to
    clean up.
    """
    setrlimit(RLIMIT_CPU, (limit, limit + recovery_time))
    signal(SIGXCPU, _xcpuhandler)


def disk_limit(limit=1e9):
    """
    Sets a maximum file size that can be written for the program.
    """
    setrlimit(RLIMIT_FSIZE, (int(limit), int(limit + 2e6)))
    # error caught by normal IO handlers


def memory_limit(limit=2e9):
    """
    Sets a maximum memory limit for the program.
    """
    setrlimit(RLIMIT_DATA, (int(limit), int(limit + 2e6)))