@@ -61,13 +61,19 @@ msgid ""
61
61
"bound, such as file operations or making network requests, where much of the"
62
62
" time is spent waiting for external resources."
63
63
msgstr ""
64
+ ":mod:`!threading` 模块提供了一种在单个进程内部并发地运行多个 `线程 "
65
+ "<https://en.wikipedia.org/wiki/Thread_(computing)>`_ (从进程分出的更小单位) 的方式。 "
66
+ "它允许创建和管理线程,以便能够平行地执行多个任务,并共享内存空间。 线程特别适用于 I/O "
67
+ "密集型的任务,如文件操作或发送网络请求,在此类任务中大部分时间都会消耗于等待外部资源。"
64
68
65
69
#: ../../library/threading.rst:27
66
70
msgid ""
67
71
"A typical use case for :mod:`!threading` includes managing a pool of worker "
68
72
"threads that can process multiple tasks concurrently. Here's a basic "
69
73
"example of creating and starting threads using :class:`~threading.Thread`::"
70
74
msgstr ""
75
+ "典型的 :mod:`!threading` 使用场景包括管理一个工作线程池来并发地处理多个任务。 下面是一个使用 "
76
+ ":class:`~threading.Thread` 创建并启动线程的简单示例::"
71
77
72
78
#: ../../library/threading.rst:31
73
79
msgid ""
@@ -100,6 +106,34 @@ msgid ""
100
106
"for t in threads:\n"
101
107
" t.join()"
102
108
msgstr ""
109
+ "import threading\n"
110
+ "import time\n"
111
+ "\n"
112
+ "def crawl(link, delay=3):\n"
113
+ " print(f\" crawl started for {link}\" )\n"
114
+ " time.sleep(delay) # 阻塞 I/O (模拟网络请求)\n"
115
+ " print(f\" crawl ended for {link}\" )\n"
116
+ "\n"
117
+ "links = [\n"
118
+ "\" https://python.org\" ,\n"
119
+ "\" https://docs.python.org\" ,\n"
120
+ "\" https://peps.python.org\" ,\n"
121
+ "]\n"
122
+ "\n"
123
+ "# 针对每个链接启动线程\n"
124
+ "threads = []\n"
125
+ "for link in links:\n"
126
+ " # 使用 `args` 传入位置参数并使用 `kwargs` 传入关键字参数\n"
127
+ " t = threading.Thread(target=crawl, args=(link,), kwargs={\" delay\" : 2})\n"
128
+ " threads.append(t)\n"
129
+ "\n"
130
+ "# 启动每个线程\n"
131
+ "for t in threads:\n"
132
+ " t.start()\n"
133
+ "\n"
134
+ "# 等待所有线程结束\n"
135
+ "for t in threads:\n"
136
+ " t.join()"
103
137
104
138
#: ../../library/threading.rst:60
105
139
msgid "This module used to be optional, it is now always available."
@@ -166,13 +200,18 @@ msgid ""
166
200
"bytecode at a time. Despite this, threads remain a useful tool for achieving"
167
201
" concurrency in many scenarios."
168
202
msgstr ""
203
+ "与使用多个进程来绕过 :term:`global interpreter lock` (GIL) 的 :mod:`multiprocessing` "
204
+ "模块不同,threading 模块是在单个进程内部操作的,这意味着所有线程共享相同的内存空间。 不过,对于 CPU 密集型任务来说 GIL 会限制 "
205
+ "threading 带来的性能提升,因为在同一时刻只有一个线程能执行 Python 字节码。 尽管如此,在许多场景中线程仍然是实现并发的有用工具。"
169
206
170
207
#: ../../library/threading.rst:105
171
208
msgid ""
172
209
"As of Python 3.13, experimental :term:`free-threaded <free threading>` "
173
210
"builds can disable the GIL, enabling true parallel execution of threads, but"
174
211
" this feature is not available by default (see :pep:`703`)."
175
212
msgstr ""
213
+ "对于 Python 3.13,实验性的 :term:`自由线程 <free threading>` 构建版可以禁用 "
214
+ "GIL,启用真正的线程并行执行,但此特性在默认情况下不可用 (参见 :pep:`703`)。"
176
215
177
216
#: ../../library/threading.rst:112
178
217
msgid "Reference"
@@ -199,6 +238,8 @@ msgid ""
199
238
"through the :mod:`!threading` module, a dummy thread object with limited "
200
239
"functionality is returned."
201
240
msgstr ""
241
+ "返回当前 :class:`Thread` 对象,与调用方的控制线程。 如果调用方的控制线程不是通过 :mod:`!threading` "
242
+ "模块创建的,则会返回一个功能受限的假线程对象。"
202
243
203
244
#: ../../library/threading.rst:132
204
245
msgid ""
@@ -325,12 +366,14 @@ msgid ""
325
366
"module. The *func* will be passed to :func:`sys.settrace` for each thread, "
326
367
"before its :meth:`~Thread.run` method is called."
327
368
msgstr ""
369
+ "为所有从 :mod:`!threading` 模块启动的线程设置追踪函数,在每个线程的 :meth:`~Thread.run` "
370
+ "方法被调用前,*func* 会被传递给 :func:`sys.settrace`。"
328
371
329
372
#: ../../library/threading.rst:230
330
373
msgid ""
331
374
"Set a trace function for all threads started from the :mod:`!threading` "
332
375
"module and all Python threads that are currently executing."
333
- msgstr ""
376
+ msgstr "为从 :mod:`!threading` 模块启动的所有线程以及当前正在执行的所有 Python 线程设置追踪函数。 "
334
377
335
378
#: ../../library/threading.rst:233
336
379
msgid ""
@@ -349,12 +392,14 @@ msgid ""
349
392
"module. The *func* will be passed to :func:`sys.setprofile` for each "
350
393
"thread, before its :meth:`~Thread.run` method is called."
351
394
msgstr ""
395
+ "为从 :mod:`!threading` 模块启动的所有线程设置性能分析函数。 在每个线程的 :meth:`~Thread.run` "
396
+ "方法被调用前,*func* 会被传递给 :func:`sys.setprofile`。"
352
397
353
398
#: ../../library/threading.rst:259
354
399
msgid ""
355
400
"Set a profile function for all threads started from the :mod:`!threading` "
356
401
"module and all Python threads that are currently executing."
357
- msgstr ""
402
+ msgstr "为从 :mod:`!threading` 模块启动的所有线程和当前正在执行和的所有 Python 线程设置性能分析函数。 "
358
403
359
404
#: ../../library/threading.rst:262
360
405
msgid ""
@@ -709,7 +754,7 @@ msgstr "一个代表线程本地数据的类。"
709
754
710
755
#: ../../library/threading.rst:457
711
756
msgid "Thread objects"
712
- msgstr "Thread 对象 "
757
+ msgstr "线程对象 "
713
758
714
759
#: ../../library/threading.rst:459
715
760
msgid ""
@@ -1058,7 +1103,7 @@ msgstr "已被弃用的 :attr:`~Thread.daemon` 的取值/设值 API;请改为
1058
1103
1059
1104
#: ../../library/threading.rst:676
1060
1105
msgid "Lock objects"
1061
- msgstr "Lock 对象 "
1106
+ msgstr "锁对象 "
1062
1107
1063
1108
#: ../../library/threading.rst:678
1064
1109
msgid ""