|
6 | 6 | # Translators: |
7 | 7 | # python-doc bot, 2025 |
8 | 8 | # Rainer Terroso, 2025 |
| 9 | +# Marcos Moraes, 2026 |
9 | 10 | # |
10 | 11 | #, fuzzy |
11 | 12 | msgid "" |
12 | 13 | msgstr "" |
13 | 14 | "Project-Id-Version: Python 3.14\n" |
14 | 15 | "Report-Msgid-Bugs-To: \n" |
15 | | -"POT-Creation-Date: 2025-12-31 14:15+0000\n" |
| 16 | +"POT-Creation-Date: 2026-03-21 14:21+0000\n" |
16 | 17 | "PO-Revision-Date: 2025-09-16 00:00+0000\n" |
17 | | -"Last-Translator: Rainer Terroso, 2025\n" |
| 18 | +"Last-Translator: Marcos Moraes, 2026\n" |
18 | 19 | "Language-Team: Portuguese (Brazil) (https://app.transifex.com/python-doc/" |
19 | 20 | "teams/5390/pt_BR/)\n" |
20 | 21 | "MIME-Version: 1.0\n" |
@@ -339,6 +340,7 @@ msgstr "" |
339 | 340 | "asyncio.run(main())" |
340 | 341 |
|
341 | 342 | #: ../../library/asyncio-dev.rst:174 ../../library/asyncio-dev.rst:219 |
| 343 | +#: ../../library/asyncio-dev.rst:393 ../../library/asyncio-dev.rst:450 |
342 | 344 | msgid "Output::" |
343 | 345 | msgstr "Saída::" |
344 | 346 |
|
@@ -497,3 +499,293 @@ msgstr "" |
497 | 499 | " File \"../t.py\", line 4, in bug\n" |
498 | 500 | " raise Exception(\"not consumed\")\n" |
499 | 501 | "Exception: not consumed" |
| 502 | + |
| 503 | +#: ../../library/asyncio-dev.rst:254 |
| 504 | +msgid "Asynchronous generators best practices" |
| 505 | +msgstr "" |
| 506 | + |
| 507 | +#: ../../library/asyncio-dev.rst:256 |
| 508 | +msgid "" |
| 509 | +"Writing correct and efficient asyncio code requires awareness of certain " |
| 510 | +"pitfalls. This section outlines essential best practices that can save you " |
| 511 | +"hours of debugging." |
| 512 | +msgstr "" |
| 513 | + |
| 514 | +#: ../../library/asyncio-dev.rst:261 |
| 515 | +msgid "Close asynchronous generators explicitly" |
| 516 | +msgstr "" |
| 517 | + |
| 518 | +#: ../../library/asyncio-dev.rst:263 |
| 519 | +msgid "" |
| 520 | +"It is recommended to manually close the :term:`asynchronous generator " |
| 521 | +"<asynchronous generator iterator>`. If a generator exits early - for " |
| 522 | +"example, due to an exception raised in the body of an ``async for`` loop - " |
| 523 | +"its asynchronous cleanup code may run in an unexpected context. This can " |
| 524 | +"occur after the tasks it depends on have completed, or during the event loop " |
| 525 | +"shutdown when the async-generator's garbage collection hook is called." |
| 526 | +msgstr "" |
| 527 | + |
| 528 | +#: ../../library/asyncio-dev.rst:271 |
| 529 | +msgid "" |
| 530 | +"To avoid this, explicitly close the generator by calling its :meth:`~agen." |
| 531 | +"aclose` method, or use the :func:`contextlib.aclosing` context manager::" |
| 532 | +msgstr "" |
| 533 | + |
| 534 | +#: ../../library/asyncio-dev.rst:275 |
| 535 | +msgid "" |
| 536 | +"import asyncio\n" |
| 537 | +"import contextlib\n" |
| 538 | +"\n" |
| 539 | +"async def gen():\n" |
| 540 | +" yield 1\n" |
| 541 | +" yield 2\n" |
| 542 | +"\n" |
| 543 | +"async def func():\n" |
| 544 | +" async with contextlib.aclosing(gen()) as g:\n" |
| 545 | +" async for x in g:\n" |
| 546 | +" break # Don't iterate until the end\n" |
| 547 | +"\n" |
| 548 | +"asyncio.run(func())" |
| 549 | +msgstr "" |
| 550 | + |
| 551 | +#: ../../library/asyncio-dev.rst:289 |
| 552 | +msgid "" |
| 553 | +"As noted above, the cleanup code for these asynchronous generators is " |
| 554 | +"deferred. The following example demonstrates that the finalization of an " |
| 555 | +"asynchronous generator can occur in an unexpected order::" |
| 556 | +msgstr "" |
| 557 | + |
| 558 | +#: ../../library/asyncio-dev.rst:293 |
| 559 | +msgid "" |
| 560 | +"import asyncio\n" |
| 561 | +"work_done = False\n" |
| 562 | +"\n" |
| 563 | +"async def cursor():\n" |
| 564 | +" try:\n" |
| 565 | +" yield 1\n" |
| 566 | +" finally:\n" |
| 567 | +" assert work_done\n" |
| 568 | +"\n" |
| 569 | +"async def rows():\n" |
| 570 | +" global work_done\n" |
| 571 | +" try:\n" |
| 572 | +" yield 2\n" |
| 573 | +" finally:\n" |
| 574 | +" await asyncio.sleep(0.1) # immitate some async work\n" |
| 575 | +" work_done = True\n" |
| 576 | +"\n" |
| 577 | +"\n" |
| 578 | +"async def main():\n" |
| 579 | +" async for c in cursor():\n" |
| 580 | +" async for r in rows():\n" |
| 581 | +" break\n" |
| 582 | +" break\n" |
| 583 | +"\n" |
| 584 | +"asyncio.run(main())" |
| 585 | +msgstr "" |
| 586 | + |
| 587 | +#: ../../library/asyncio-dev.rst:319 |
| 588 | +msgid "For this example, we get the following output::" |
| 589 | +msgstr "" |
| 590 | + |
| 591 | +#: ../../library/asyncio-dev.rst:321 |
| 592 | +msgid "" |
| 593 | +"unhandled exception during asyncio.run() shutdown\n" |
| 594 | +"task: <Task finished name='Task-3' coro=<<async_generator_athrow without " |
| 595 | +"__name__>()> exception=AssertionError()>\n" |
| 596 | +"Traceback (most recent call last):\n" |
| 597 | +" File \"example.py\", line 6, in cursor\n" |
| 598 | +" yield 1\n" |
| 599 | +"asyncio.exceptions.CancelledError\n" |
| 600 | +"\n" |
| 601 | +"During handling of the above exception, another exception occurred:\n" |
| 602 | +"\n" |
| 603 | +"Traceback (most recent call last):\n" |
| 604 | +" File \"example.py\", line 8, in cursor\n" |
| 605 | +" assert work_done\n" |
| 606 | +" ^^^^^^^^^\n" |
| 607 | +"AssertionError" |
| 608 | +msgstr "" |
| 609 | + |
| 610 | +#: ../../library/asyncio-dev.rst:336 |
| 611 | +msgid "" |
| 612 | +"The ``cursor()`` asynchronous generator was finalized before the ``rows`` " |
| 613 | +"generator - an unexpected behavior." |
| 614 | +msgstr "" |
| 615 | + |
| 616 | +#: ../../library/asyncio-dev.rst:339 |
| 617 | +msgid "" |
| 618 | +"The example can be fixed by explicitly closing the ``cursor`` and ``rows`` " |
| 619 | +"async-generators::" |
| 620 | +msgstr "" |
| 621 | + |
| 622 | +#: ../../library/asyncio-dev.rst:342 |
| 623 | +msgid "" |
| 624 | +"async def main():\n" |
| 625 | +" async with contextlib.aclosing(cursor()) as cursor_gen:\n" |
| 626 | +" async for c in cursor_gen:\n" |
| 627 | +" async with contextlib.aclosing(rows()) as rows_gen:\n" |
| 628 | +" async for r in rows_gen:\n" |
| 629 | +" break\n" |
| 630 | +" break" |
| 631 | +msgstr "" |
| 632 | + |
| 633 | +#: ../../library/asyncio-dev.rst:352 |
| 634 | +msgid "Create asynchronous generators only when the event loop is running" |
| 635 | +msgstr "" |
| 636 | + |
| 637 | +#: ../../library/asyncio-dev.rst:354 |
| 638 | +msgid "" |
| 639 | +"It is recommended to create :term:`asynchronous generators <asynchronous " |
| 640 | +"generator iterator>` only after the event loop has been created." |
| 641 | +msgstr "" |
| 642 | + |
| 643 | +#: ../../library/asyncio-dev.rst:358 |
| 644 | +msgid "" |
| 645 | +"To ensure that asynchronous generators close reliably, the event loop uses " |
| 646 | +"the :func:`sys.set_asyncgen_hooks` function to register callback functions. " |
| 647 | +"These callbacks update the list of running asynchronous generators to keep " |
| 648 | +"it in a consistent state." |
| 649 | +msgstr "" |
| 650 | + |
| 651 | +#: ../../library/asyncio-dev.rst:363 |
| 652 | +msgid "" |
| 653 | +"When the :meth:`loop.shutdown_asyncgens() <asyncio.loop.shutdown_asyncgens>` " |
| 654 | +"function is called, the running generators are stopped gracefully and the " |
| 655 | +"list is cleared." |
| 656 | +msgstr "" |
| 657 | + |
| 658 | +#: ../../library/asyncio-dev.rst:367 |
| 659 | +msgid "" |
| 660 | +"The asynchronous generator invokes the corresponding system hook during its " |
| 661 | +"first iteration. At the same time, the generator records that the hook has " |
| 662 | +"been called and does not call it again." |
| 663 | +msgstr "" |
| 664 | + |
| 665 | +#: ../../library/asyncio-dev.rst:371 |
| 666 | +msgid "" |
| 667 | +"Therefore, if iteration begins before the event loop is created, the event " |
| 668 | +"loop will not be able to add the generator to its list of active generators " |
| 669 | +"because the hooks are set after the generator attempts to call them. " |
| 670 | +"Consequently, the event loop will not be able to terminate the generator if " |
| 671 | +"necessary." |
| 672 | +msgstr "" |
| 673 | + |
| 674 | +#: ../../library/asyncio-dev.rst:377 |
| 675 | +msgid "Consider the following example::" |
| 676 | +msgstr "Considere o exemplo a seguir::" |
| 677 | + |
| 678 | +#: ../../library/asyncio-dev.rst:379 |
| 679 | +msgid "" |
| 680 | +"import asyncio\n" |
| 681 | +"\n" |
| 682 | +"async def agenfn():\n" |
| 683 | +" try:\n" |
| 684 | +" yield 10\n" |
| 685 | +" finally:\n" |
| 686 | +" await asyncio.sleep(0)\n" |
| 687 | +"\n" |
| 688 | +"\n" |
| 689 | +"with asyncio.Runner() as runner:\n" |
| 690 | +" agen = agenfn()\n" |
| 691 | +" print(runner.run(anext(agen)))\n" |
| 692 | +" del agen" |
| 693 | +msgstr "" |
| 694 | + |
| 695 | +#: ../../library/asyncio-dev.rst:395 |
| 696 | +msgid "" |
| 697 | +"10\n" |
| 698 | +"Exception ignored while closing generator <async_generator object agenfn at " |
| 699 | +"0x000002F71CD10D70>:\n" |
| 700 | +"Traceback (most recent call last):\n" |
| 701 | +" File \"example.py\", line 13, in <module>\n" |
| 702 | +" del agen\n" |
| 703 | +" ^^^^\n" |
| 704 | +"RuntimeError: async generator ignored GeneratorExit" |
| 705 | +msgstr "" |
| 706 | + |
| 707 | +#: ../../library/asyncio-dev.rst:403 |
| 708 | +msgid "This example can be fixed as follows::" |
| 709 | +msgstr "" |
| 710 | + |
| 711 | +#: ../../library/asyncio-dev.rst:405 |
| 712 | +msgid "" |
| 713 | +"import asyncio\n" |
| 714 | +"\n" |
| 715 | +"async def agenfn():\n" |
| 716 | +" try:\n" |
| 717 | +" yield 10\n" |
| 718 | +" finally:\n" |
| 719 | +" await asyncio.sleep(0)\n" |
| 720 | +"\n" |
| 721 | +"async def main():\n" |
| 722 | +" agen = agenfn()\n" |
| 723 | +" print(await anext(agen))\n" |
| 724 | +" del agen\n" |
| 725 | +"\n" |
| 726 | +"asyncio.run(main())" |
| 727 | +msgstr "" |
| 728 | + |
| 729 | +#: ../../library/asyncio-dev.rst:422 |
| 730 | +msgid "Avoid concurrent iteration and closure of the same generator" |
| 731 | +msgstr "" |
| 732 | + |
| 733 | +#: ../../library/asyncio-dev.rst:424 |
| 734 | +msgid "" |
| 735 | +"Async generators may be reentered while another :meth:`~agen.__anext__` / :" |
| 736 | +"meth:`~agen.athrow` / :meth:`~agen.aclose` call is in progress. This may " |
| 737 | +"lead to an inconsistent state of the async generator and can cause errors." |
| 738 | +msgstr "" |
| 739 | + |
| 740 | +#: ../../library/asyncio-dev.rst:429 |
| 741 | +msgid "Let's consider the following example::" |
| 742 | +msgstr "" |
| 743 | + |
| 744 | +#: ../../library/asyncio-dev.rst:431 |
| 745 | +msgid "" |
| 746 | +"import asyncio\n" |
| 747 | +"\n" |
| 748 | +"async def consumer():\n" |
| 749 | +" for idx in range(100):\n" |
| 750 | +" await asyncio.sleep(0)\n" |
| 751 | +" message = yield idx\n" |
| 752 | +" print('received', message)\n" |
| 753 | +"\n" |
| 754 | +"async def amain():\n" |
| 755 | +" agenerator = consumer()\n" |
| 756 | +" await agenerator.asend(None)\n" |
| 757 | +"\n" |
| 758 | +" fa = asyncio.create_task(agenerator.asend('A'))\n" |
| 759 | +" fb = asyncio.create_task(agenerator.asend('B'))\n" |
| 760 | +" await fa\n" |
| 761 | +" await fb\n" |
| 762 | +"\n" |
| 763 | +"asyncio.run(amain())" |
| 764 | +msgstr "" |
| 765 | + |
| 766 | +#: ../../library/asyncio-dev.rst:452 |
| 767 | +msgid "" |
| 768 | +"received A\n" |
| 769 | +"Traceback (most recent call last):\n" |
| 770 | +" File \"test.py\", line 38, in <module>\n" |
| 771 | +" asyncio.run(amain())\n" |
| 772 | +" ~~~~~~~~~~~^^^^^^^^^\n" |
| 773 | +" File \"Lib/asyncio/runners.py\", line 204, in run\n" |
| 774 | +" return runner.run(main)\n" |
| 775 | +" ~~~~~~~~~~^^^^^^\n" |
| 776 | +" File \"Lib/asyncio/runners.py\", line 127, in run\n" |
| 777 | +" return self._loop.run_until_complete(task)\n" |
| 778 | +" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n" |
| 779 | +" File \"Lib/asyncio/base_events.py\", line 719, in run_until_complete\n" |
| 780 | +" return future.result()\n" |
| 781 | +" ~~~~~~~~~~~~~^^\n" |
| 782 | +" File \"test.py\", line 36, in amain\n" |
| 783 | +" await fb\n" |
| 784 | +"RuntimeError: anext(): asynchronous generator is already running" |
| 785 | +msgstr "" |
| 786 | + |
| 787 | +#: ../../library/asyncio-dev.rst:471 |
| 788 | +msgid "" |
| 789 | +"Therefore, it is recommended to avoid using asynchronous generators in " |
| 790 | +"parallel tasks or across multiple event loops." |
| 791 | +msgstr "" |
0 commit comments