|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You want to place some limits on the memory or CPU use of a program running on |
9 |
| -Unix system. |
| 8 | +你想对在Unix系统上面运行的程序设置内存或CPU的使用限制。 |
10 | 9 |
|
11 | 10 | |
|
12 | 11 |
|
13 | 12 | ----------
|
14 | 13 | 解决方案
|
15 | 14 | ----------
|
16 |
| -The resource module can be used to perform both tasks. For example, to restrict CPU |
17 |
| -time, do the following: |
| 15 | +``resource`` 模块能同时执行这两个任务。例如,要限制CPU时间,可以像下面这样做: |
18 | 16 |
|
19 |
| -import signal |
20 |
| -import resource |
21 |
| -import os |
| 17 | +.. code-block:: python |
22 | 18 |
|
23 |
| -def time_exceeded(signo, frame): |
24 |
| - print("Time's up!") |
25 |
| - raise SystemExit(1) |
| 19 | + import signal |
| 20 | + import resource |
| 21 | + import os |
26 | 22 |
|
27 |
| -def set_max_runtime(seconds): |
28 |
| - # Install the signal handler and set a resource limit |
29 |
| - soft, hard = resource.getrlimit(resource.RLIMIT_CPU) |
30 |
| - resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) |
31 |
| - signal.signal(signal.SIGXCPU, time_exceeded) |
| 23 | + def time_exceeded(signo, frame): |
| 24 | + print("Time's up!") |
| 25 | + raise SystemExit(1) |
32 | 26 |
|
33 |
| -if __name__ == '__main__': |
34 |
| - set_max_runtime(15) |
35 |
| - while True: |
36 |
| - pass |
| 27 | + def set_max_runtime(seconds): |
| 28 | + # Install the signal handler and set a resource limit |
| 29 | + soft, hard = resource.getrlimit(resource.RLIMIT_CPU) |
| 30 | + resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) |
| 31 | + signal.signal(signal.SIGXCPU, time_exceeded) |
37 | 32 |
|
38 |
| -When this runs, the SIGXCPU signal is generated when the time expires. The program |
39 |
| -can then clean up and exit. |
40 |
| -To restrict memory use, put a limit on the total address space in use. For example: |
| 33 | + if __name__ == '__main__': |
| 34 | + set_max_runtime(15) |
| 35 | + while True: |
| 36 | + pass |
41 | 37 |
|
42 |
| -import resource |
| 38 | +程序运行时,``SIGXCPU`` 信号在时间过期时被生成,然后执行清理并退出。 |
43 | 39 |
|
44 |
| -def limit_memory(maxsize): |
45 |
| - soft, hard = resource.getrlimit(resource.RLIMIT_AS) |
46 |
| - resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard)) |
| 40 | +要限制内存使用,设置可使用的总内存值即可,如下: |
47 | 41 |
|
48 |
| -With a memory limit in place, programs will start generating MemoryError exceptions |
49 |
| -when no more memory is available. |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + import resource |
| 45 | +
|
| 46 | + def limit_memory(maxsize): |
| 47 | + soft, hard = resource.getrlimit(resource.RLIMIT_AS) |
| 48 | + resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard)) |
| 49 | +
|
| 50 | +像这样设置了内存限制后,程序运行到没有多余内存时会抛出 ``MemoryError`` 异常。 |
50 | 51 |
|
51 | 52 | |
|
52 | 53 |
|
53 | 54 | ----------
|
54 | 55 | 讨论
|
55 | 56 | ----------
|
56 |
| -In this recipe, the setrlimit() function is used to set a soft and hard limit on a particular |
57 |
| -resource. The soft limit is a value upon which the operating system will typically restrict |
58 |
| -or notify the process via a signal. The hard limit represents an upper bound on the values |
59 |
| -that may be used for the soft limit. Typically, this is controlled by a system-wide pa‐ |
60 |
| -rameter set by the system administrator. Although the hard limit can be lowered, it can |
61 |
| -never be raised by user processes (even if the process lowered itself). |
62 |
| -The setrlimit() function can additionally be used to set limits on things such as the |
63 |
| -number of child processes, number of open files, and similar system resources. Consult |
64 |
| -the documentation for the resource module for further details. |
65 |
| -Be aware that this recipe only works on Unix systems, and that it might not work on all |
66 |
| -of them. For example, when tested, it works on Linux but not on OS X. |
| 57 | +在本节例子中,``setrlimit()`` 函数被用来设置特定资源上面的软限制和硬限制。 |
| 58 | +软限制是一个值,当超过这个值的时候操作系统通常会发送一个信号来限制或通知该进程。 |
| 59 | +硬限制是用来指定软限制能设定的最大值。通常来讲,这个由系统管理员通过设置系统级参数来决定。 |
| 60 | +尽管硬限制可以改小一点,但是最好不要使用用户进程去修改。 |
| 61 | + |
| 62 | +``setrlimit()`` 函数还能被用来设置子进程数量、打开文件数以及类似系统资源的限制。 |
| 63 | +更多详情请参考 ``resource`` 模块的文档。 |
67 | 64 |
|
| 65 | +需要注意的是本节内容只能适用于Unix系统,并且不保证所有系统都能如期工作。 |
| 66 | +比如我们在测试的时候,它能在Linux上面正常运行,但是在OS X上却不能。 |
0 commit comments