Skip to content

Commit 0d182a8

Browse files
committed
15.8小节完成
1 parent db63510 commit 0d182a8

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

source/c15/p08_mix_threads_from_c_and_python.rst

+25-29
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
----------
66
问题
77
----------
8-
You have a program that involves a mix of C, Python, and threads, but some of the
9-
threads are created from C outside the control of the Python interpreter. Moreover,
10-
certain threads utilize functions in the Python C API.
8+
你有一个程序需要混合使用C、Python和线程,
9+
有些线程是在C中创建的,超出了Python解释器的控制范围。
10+
并且一些线程还使用了Python C API中的函数。
1111

1212
|
1313
1414
----------
1515
解决方案
1616
----------
17-
If you’re going to mix C, Python, and threads together, you need to make sure you
18-
properly initialize and manage Python’s global interpreter lock (GIL). To do this, include
19-
the following code somewhere in your C code and make sure it’s called prior to creation
20-
of any threads:
17+
如果你想将C、Python和线程混合在一起,你需要确保正确的初始化和管理Python的全局解释器锁(GIL)。
18+
要想这样做,可以将下列代码放到你的C代码中并确保它在任何线程被创建之前被调用。
2119

22-
#include <Python.h>
20+
::
2321

24-
...
25-
if (!PyEval_ThreadsInitialized()) {
26-
PyEval_InitThreads();
27-
}
28-
...
22+
#include <Python.h>
23+
...
24+
if (!PyEval_ThreadsInitialized()) {
25+
PyEval_InitThreads();
26+
}
27+
...
2928

30-
For any C code that involves Python objects or the Python C API, make sure you prop‐
31-
erly acquire and release the GIL first. This is done using PyGILState_Ensure() and
32-
PyGILState_Release(), as shown in the following:
29+
对于任何调用Python对象或Python C API的C代码,确保你首先已经正确地获取和释放了GIL。
30+
这可以用 ``PyGILState_Ensure()`` 和 ``PyGILState_Release()`` 来做到,如下所示:
31+
32+
::
3333

3434
...
3535
/* Make sure we own the GIL */
@@ -41,22 +41,18 @@ PyGILState_Release(), as shown in the following:
4141
PyGILState_Release(state);
4242
...
4343

44-
Every call to PyGILState_Ensure() must have a matching call to PyGILState_Re
45-
lease().
44+
每次调用 ``PyGILState_Ensure()`` 都要相应的调用 ``PyGILState_Release()`` .
4645

4746
|
4847
4948
----------
5049
讨论
5150
----------
52-
In advanced applications involving C and Python, it is not uncommon to have many
53-
things going on at once—possibly involving a mix of a C code, Python code, C threads,
54-
and Python threads. As long as you diligently make sure the interpreter is properly
55-
initialized and that C code involving the interpreter has the proper GIL management
56-
calls, it all should work.
57-
Be aware that the PyGILState_Ensure() call does not immediately preempt or interrupt
58-
the interpreter. If other code is currently executing, this function will block until that
59-
code decides to release the GIL. Internally, the interpreter performs periodic thread
60-
switching, so even if another thread is executing, the caller will eventually get to run
61-
(although it may have to wait for a while first).
62-
51+
在涉及到C和Python的高级程序中,很多事情一起做是很常见的——
52+
可能是对C、Python、C线程、Python线程的混合使用。
53+
只要你确保解释器被正确的初始化,并且涉及到解释器的C代码执行了正确的GIL管理,应该没什么问题。
54+
55+
要注意的是调用 ``PyGILState_Ensure()`` 并不会立刻抢占或中断解释器。
56+
如果有其他代码正在执行,这个函数被中断知道那个执行代码释放掉GIL。
57+
在内部,解释器会执行周期性的线程切换,因此如果其他线程在执行,
58+
调用者最终还是可以运行的(尽管可能要先等一会)。

0 commit comments

Comments
 (0)