Skip to content

Commit bd0b2f8

Browse files
committed
13.14小节完成
1 parent 6709b7b commit bd0b2f8

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

source/c13/p13_making_stopwatch_timer.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
==============================
2-
13.13 记录程序执行的时间
2+
13.13 实现一个计时器
33
==============================
44

55
----------

source/c13/p14_putting_limits_on_memory_and_cpu_usage.rst

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,62 @@
55
----------
66
问题
77
----------
8-
You want to place some limits on the memory or CPU use of a program running on
9-
Unix system.
8+
你想对在Unix系统上面运行的程序设置内存或CPU的使用限制。
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
The resource module can be used to perform both tasks. For example, to restrict CPU
17-
time, do the following:
15+
``resource`` 模块能同时执行这两个任务。例如,要限制CPU时间,可以像下面这样做:
1816

19-
import signal
20-
import resource
21-
import os
17+
.. code-block:: python
2218
23-
def time_exceeded(signo, frame):
24-
print("Time's up!")
25-
raise SystemExit(1)
19+
import signal
20+
import resource
21+
import os
2622
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)
3226
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)
3732
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
4137
42-
import resource
38+
程序运行时,``SIGXCPU`` 信号在时间过期时被生成,然后执行清理并退出。
4339

44-
def limit_memory(maxsize):
45-
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
46-
resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard))
40+
要限制内存使用,设置可使用的总内存值即可,如下:
4741

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`` 异常。
5051

5152
|
5253
5354
----------
5455
讨论
5556
----------
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`` 模块的文档。
6764

65+
需要注意的是本节内容只能适用于Unix系统,并且不保证所有系统都能如期工作。
66+
比如我们在测试的时候,它能在Linux上面正常运行,但是在OS X上却不能。

0 commit comments

Comments
 (0)