|
5 | 5 | msgstr "" |
6 | 6 | "Project-Id-Version: Python 3.13\n" |
7 | 7 | "Report-Msgid-Bugs-To: \n" |
8 | | -"POT-Creation-Date: 2024-09-24 23:08+0000\n" |
| 8 | +"POT-Creation-Date: 2024-10-31 00:13+0000\n" |
9 | 9 | "PO-Revision-Date: 2018-07-15 18:56+0800\n" |
10 | 10 | "Last-Translator: \n" |
11 | 11 | "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" |
@@ -203,121 +203,161 @@ msgstr "" |
203 | 203 |
|
204 | 204 | #: ../../library/contextvars.rst:147 |
205 | 205 | msgid "" |
206 | | -"Every thread will have a different top-level :class:`~contextvars.Context` " |
207 | | -"object. This means that a :class:`ContextVar` object behaves in a similar " |
208 | | -"fashion to :func:`threading.local` when values are assigned in different " |
209 | | -"threads." |
| 206 | +"Each thread has its own effective stack of :class:`!Context` objects. The :" |
| 207 | +"term:`current context` is the :class:`!Context` object at the top of the " |
| 208 | +"current thread's stack. All :class:`!Context` objects in the stacks are " |
| 209 | +"considered to be *entered*." |
210 | 210 | msgstr "" |
211 | 211 |
|
212 | 212 | #: ../../library/contextvars.rst:152 |
213 | | -msgid "Context implements the :class:`collections.abc.Mapping` interface." |
| 213 | +msgid "" |
| 214 | +"*Entering* a context, which can be done by calling its :meth:`~Context.run` " |
| 215 | +"method, makes the context the current context by pushing it onto the top of " |
| 216 | +"the current thread's context stack." |
214 | 217 | msgstr "" |
215 | 218 |
|
216 | 219 | #: ../../library/contextvars.rst:156 |
217 | 220 | msgid "" |
218 | | -"Execute ``callable(*args, **kwargs)`` code in the context object the *run* " |
219 | | -"method is called on. Return the result of the execution or propagate an " |
220 | | -"exception if one occurred." |
| 221 | +"*Exiting* from the current context, which can be done by returning from the " |
| 222 | +"callback passed to the :meth:`~Context.run` method, restores the current " |
| 223 | +"context to what it was before the context was entered by popping the context " |
| 224 | +"off the top of the context stack." |
221 | 225 | msgstr "" |
222 | 226 |
|
223 | | -#: ../../library/contextvars.rst:160 |
| 227 | +#: ../../library/contextvars.rst:161 |
224 | 228 | msgid "" |
225 | | -"Any changes to any context variables that *callable* makes will be contained " |
226 | | -"in the context object::" |
| 229 | +"Since each thread has its own context stack, :class:`ContextVar` objects " |
| 230 | +"behave in a similar fashion to :func:`threading.local` when values are " |
| 231 | +"assigned in different threads." |
227 | 232 | msgstr "" |
228 | 233 |
|
229 | | -#: ../../library/contextvars.rst:163 |
| 234 | +#: ../../library/contextvars.rst:165 |
230 | 235 | msgid "" |
231 | | -"var = ContextVar('var')\n" |
| 236 | +"Attempting to enter an already entered context, including contexts entered " |
| 237 | +"in other threads, raises a :exc:`RuntimeError`." |
| 238 | +msgstr "" |
| 239 | + |
| 240 | +#: ../../library/contextvars.rst:168 |
| 241 | +msgid "After exiting a context, it can later be re-entered (from any thread)." |
| 242 | +msgstr "" |
| 243 | + |
| 244 | +#: ../../library/contextvars.rst:170 |
| 245 | +msgid "" |
| 246 | +"Any changes to :class:`ContextVar` values via the :meth:`ContextVar.set` " |
| 247 | +"method are recorded in the current context. The :meth:`ContextVar.get` " |
| 248 | +"method returns the value associated with the current context. Exiting a " |
| 249 | +"context effectively reverts any changes made to context variables while the " |
| 250 | +"context was entered (if needed, the values can be restored by re-entering " |
| 251 | +"the context)." |
| 252 | +msgstr "" |
| 253 | + |
| 254 | +#: ../../library/contextvars.rst:177 |
| 255 | +msgid "Context implements the :class:`collections.abc.Mapping` interface." |
| 256 | +msgstr "" |
| 257 | + |
| 258 | +#: ../../library/contextvars.rst:181 |
| 259 | +msgid "" |
| 260 | +"Enters the Context, executes ``callable(*args, **kwargs)``, then exits the " |
| 261 | +"Context. Returns *callable*'s return value, or propagates an exception if " |
| 262 | +"one occurred." |
| 263 | +msgstr "" |
| 264 | + |
| 265 | +#: ../../library/contextvars.rst:185 |
| 266 | +#, fuzzy |
| 267 | +msgid "Example:" |
| 268 | +msgstr "舉例來說: ::" |
| 269 | + |
| 270 | +#: ../../library/contextvars.rst:187 |
| 271 | +msgid "" |
| 272 | +"import contextvars\n" |
| 273 | +"\n" |
| 274 | +"var = contextvars.ContextVar('var')\n" |
232 | 275 | "var.set('spam')\n" |
| 276 | +"print(var.get()) # 'spam'\n" |
| 277 | +"\n" |
| 278 | +"ctx = contextvars.copy_context()\n" |
233 | 279 | "\n" |
234 | 280 | "def main():\n" |
235 | 281 | " # 'var' was set to 'spam' before\n" |
236 | 282 | " # calling 'copy_context()' and 'ctx.run(main)', so:\n" |
237 | | -" # var.get() == ctx[var] == 'spam'\n" |
| 283 | +" print(var.get()) # 'spam'\n" |
| 284 | +" print(ctx[var]) # 'spam'\n" |
238 | 285 | "\n" |
239 | 286 | " var.set('ham')\n" |
240 | 287 | "\n" |
241 | 288 | " # Now, after setting 'var' to 'ham':\n" |
242 | | -" # var.get() == ctx[var] == 'ham'\n" |
243 | | -"\n" |
244 | | -"ctx = copy_context()\n" |
| 289 | +" print(var.get()) # 'ham'\n" |
| 290 | +" print(ctx[var]) # 'ham'\n" |
245 | 291 | "\n" |
246 | 292 | "# Any changes that the 'main' function makes to 'var'\n" |
247 | 293 | "# will be contained in 'ctx'.\n" |
248 | 294 | "ctx.run(main)\n" |
249 | 295 | "\n" |
250 | 296 | "# The 'main()' function was run in the 'ctx' context,\n" |
251 | 297 | "# so changes to 'var' are contained in it:\n" |
252 | | -"# ctx[var] == 'ham'\n" |
| 298 | +"print(ctx[var]) # 'ham'\n" |
253 | 299 | "\n" |
254 | 300 | "# However, outside of 'ctx', 'var' is still set to 'spam':\n" |
255 | | -"# var.get() == 'spam'" |
256 | | -msgstr "" |
257 | | - |
258 | | -#: ../../library/contextvars.rst:189 |
259 | | -msgid "" |
260 | | -"The method raises a :exc:`RuntimeError` when called on the same context " |
261 | | -"object from more than one OS thread, or when called recursively." |
| 301 | +"print(var.get()) # 'spam'" |
262 | 302 | msgstr "" |
263 | 303 |
|
264 | | -#: ../../library/contextvars.rst:195 |
| 304 | +#: ../../library/contextvars.rst:233 |
265 | 305 | msgid "Return a shallow copy of the context object." |
266 | 306 | msgstr "" |
267 | 307 |
|
268 | | -#: ../../library/contextvars.rst:199 |
| 308 | +#: ../../library/contextvars.rst:237 |
269 | 309 | msgid "" |
270 | 310 | "Return ``True`` if the *context* has a value for *var* set; return ``False`` " |
271 | 311 | "otherwise." |
272 | 312 | msgstr "" |
273 | 313 |
|
274 | | -#: ../../library/contextvars.rst:204 |
| 314 | +#: ../../library/contextvars.rst:242 |
275 | 315 | msgid "" |
276 | 316 | "Return the value of the *var* :class:`ContextVar` variable. If the variable " |
277 | 317 | "is not set in the context object, a :exc:`KeyError` is raised." |
278 | 318 | msgstr "" |
279 | 319 |
|
280 | | -#: ../../library/contextvars.rst:210 |
| 320 | +#: ../../library/contextvars.rst:248 |
281 | 321 | msgid "" |
282 | 322 | "Return the value for *var* if *var* has the value in the context object. " |
283 | 323 | "Return *default* otherwise. If *default* is not given, return ``None``." |
284 | 324 | msgstr "" |
285 | 325 |
|
286 | | -#: ../../library/contextvars.rst:216 |
| 326 | +#: ../../library/contextvars.rst:254 |
287 | 327 | msgid "Return an iterator over the variables stored in the context object." |
288 | 328 | msgstr "" |
289 | 329 |
|
290 | | -#: ../../library/contextvars.rst:221 |
| 330 | +#: ../../library/contextvars.rst:259 |
291 | 331 | msgid "Return the number of variables set in the context object." |
292 | 332 | msgstr "" |
293 | 333 |
|
294 | | -#: ../../library/contextvars.rst:225 |
| 334 | +#: ../../library/contextvars.rst:263 |
295 | 335 | msgid "Return a list of all variables in the context object." |
296 | 336 | msgstr "" |
297 | 337 |
|
298 | | -#: ../../library/contextvars.rst:229 |
| 338 | +#: ../../library/contextvars.rst:267 |
299 | 339 | msgid "Return a list of all variables' values in the context object." |
300 | 340 | msgstr "" |
301 | 341 |
|
302 | | -#: ../../library/contextvars.rst:234 |
| 342 | +#: ../../library/contextvars.rst:272 |
303 | 343 | msgid "" |
304 | 344 | "Return a list of 2-tuples containing all variables and their values in the " |
305 | 345 | "context object." |
306 | 346 | msgstr "" |
307 | 347 |
|
308 | | -#: ../../library/contextvars.rst:239 |
| 348 | +#: ../../library/contextvars.rst:277 |
309 | 349 | msgid "asyncio support" |
310 | 350 | msgstr "" |
311 | 351 |
|
312 | | -#: ../../library/contextvars.rst:241 |
| 352 | +#: ../../library/contextvars.rst:279 |
313 | 353 | msgid "" |
314 | 354 | "Context variables are natively supported in :mod:`asyncio` and are ready to " |
315 | 355 | "be used without any extra configuration. For example, here is a simple echo " |
316 | 356 | "server, that uses a context variable to make the address of a remote client " |
317 | 357 | "available in the Task that handles that client::" |
318 | 358 | msgstr "" |
319 | 359 |
|
320 | | -#: ../../library/contextvars.rst:247 |
| 360 | +#: ../../library/contextvars.rst:285 |
321 | 361 | msgid "" |
322 | 362 | "import asyncio\n" |
323 | 363 | "import contextvars\n" |
|
0 commit comments