Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python3.11新機能asyncio.TaskGroup()と2022年asyncioの"...

Python3.11新機能asyncio.TaskGroup()と2022年asyncioの"Hello-ish world"

Python 3.11 new feature asyncio.TaskGroup. "Hello-ish world" in asyncio 2022.
[PyCon JP 2022](https://2022.pycon.jp/timetable/?id=J7S98E)

Junya Fukuda

October 15, 2022
Tweet

More Decks by Junya Fukuda

Other Decks in Technology

Transcript

  1. - asyncio 基本 •ඇಉظॲཧͷίʔυΛॻͨ͘ΊͷެࣜϥΠϒϥϦ •ຖόʔδϣϯਐԽ ߴϨϕϧAPI͕ॆ࣮ - ͱͯ΋ॻ͖΍͍͢ •ຊ΍PyCon JP

    2021ͰͦΜͳτʔΫΛ͍͖ͤͯͨͩͨ͞·ͨ͠ɻͥͻʂ "4(*ΞϓϦέʔγϣϯೖ໳͜Θ͘ͳ͍BTZODJPجૅͱඇಉظ*0'BTU"1*Λྫʹ 1Z$PO+1 •ෳ਺ͷλεΫΛಉ࣌ʹॲཧ͢Δ •I/Oό΢ϯυͳॲཧʢDBΞΫηε/HTTPϦΫΤετͳͲʣͰޮՌΛൃش
  2. ( 話 ?) •େ͖͘2ͭ •Python Core Developer Yury SelivanovࢯͷπΠʔτʢasync/await ͷAuthorʣ

    •Taskgroup͕merge͞Εͨ࣌ʹڵฃͷπΠʔτΛ࿈౤͍ͯͨ͠ •υΩϡϝϯτಡΜͰ΋Α͘෼͔Βͳ͍ •ͳʹ͕ͦΜͳʹ͍͍ͷ͔͠Β…🤔
  3. •Quickstart - Example 3-2. import asyncio import time async def

    main(): print(f"{time.ctime()} Hello!") await asyncio.sleep(1.0) print(f"{time.ctime()} GoodBye!") loop = asyncio.get_event_loop() task = loop.create_task(main()) loop.run_until_complete(task) pending = asyncio.all_tasks(loop=loop) for task in pending: task.cancel() group = asyncio.gather(*pending, return_exceptions=True) loop.run_until_complete(group) loop.close()
  4. ( 話 ?) •͓ੈ࿩ʹͳ͍ͬͯΔຊʮUsing Asyncio in Pythonʯ •෼͔Βͳ͍ίʔυ͕͋ͬͨ •େ͖͘2ͭ •Quickstart

    - Example 3-2. The “Hello-ish World” of Asyncio •͜ͷίʔυΛ asyncio.TaskGroupͰ࡮৽͍ͨ͠
  5. import asyncio async def main(): async with asyncio.TaskGroup() as tg:

    task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...)) print("Both tasks have completed now.") asycio.TaskGroup
  6. •ಉ࣌ʹ࣮ߦ͢Δඇಉظؔ਺Λ༻ҙ - asycn def Ͱఆٛ͢Δίϧʔνϯ 3ͭ༻ҙ ࣄલ४උ import asyncio async

    def coro_success(): return "੒ޭ" async def coro_value_err(): raise ValueError async def coro_type_err(): raise TypeError
  7. asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠""" try: results

    = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  8. asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠""" try: results

    = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  9. asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠""" try: results

    = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  10. asyncio.gather - return_exceptions ࢦఆͳ͠ $ python310 1_asyncio_gather_except.py async def main():

    """return_exceptionsͳ͠""" try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  11. asyncio.gather - return_exceptions ࢦఆͳ͠ $ python310 1_asyncio_gather_except.py err=ValueError() $ python310

    async def main(): """return_exceptionsͳ͠""" try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  12. asyncio.gather - return_exceptions ࢦఆͳ͠ $ python310 1_asyncio_gather_except.py err=ValueError() $ python310

    ValueErrorͷΈ TypeError͸ัଊͰ͖ͳ͍ async def main(): """return_exceptionsͳ͠""" try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  13. asyncio.gather - return_exceptions ࢦఆͳ͠ $ python310 1_asyncio_gather_except.py err=ValueError() $ python310

    ValueErrorͷΈ TypeError͸ัଊͰ͖ͳ͍ ࠷ॳͷྫ֎ͷΈัଊ ͋ͱ͸ѲΓͭͿ͠·͢ async def main(): """return_exceptionsͳ͠""" try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}") asyncio.run(main()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  14. •asyncio.gather - return_exceptions ࢦఆͳ͠ •asyncio.gather - return_exceptions ࢦఆ͋Γ return_exceptions ࢦఆ͋Γͷ৔߹ɺ

    ͢΂ͯͷλεΫΛ࣮ߦͨ͠ޙʹ݁ՌΛϦετͰऔಘ͢Δ ໭Γ஋ͷϦετΛνΣοΫ͢Δඞཁ͕͋Δ
  15. asyncio.gather - return_exceptions ࢦఆ͋Γ async def main(): """return_exceptions=True͋Γ""" results =

    await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  16. asyncio.gather - return_exceptions ࢦఆ͋Γ async def main(): """return_exceptions=True͋Γ""" results =

    await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  17. asyncio.gather - return_exceptions ࢦఆ͋Γ async def main(): """return_exceptions=True͋Γ""" results =

    await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  18. asyncio.gather - return_exceptions ࢦఆ͋Γ $ python310 2_asyncio_gather_except_true.py async def main():

    """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  19. asyncio.gather - return_exceptions ࢦఆ͋Γ $ python310 2_asyncio_gather_except_true.py async def main():

    """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") results=['੒ޭ', ValueError(), TypeError()] $ python310 ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  20. asyncio.gather - return_exceptions ࢦఆ͋Γ $ python310 2_asyncio_gather_except_true.py async def main():

    """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") results=['੒ޭ', ValueError(), TypeError()] $ python310 ͢΂ͯͷ݁ՌΛϦετͰऔಘ ͲΜͳྫ֎͕ൃੜ͔ͨ֬͠ೝ͕ඞཁ ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  21. asyncio.gather - return_exceptions ࢦఆ͋Γ for result in results: match result:

    case ValueError(): print("ValueError") case TypeError(): print("TypeError") async def main(): """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  22. asyncio.gather - return_exceptions ࢦఆ͋Γ for result in results: match result:

    case ValueError(): print("ValueError") case TypeError(): print("TypeError") async def main(): """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  23. asyncio.gather - return_exceptions ࢦఆ͋Γ $ python310 2_asyncio_gather_except_true.py async def main():

    """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") for result in results: match result: case ValueError(): print("ValueError") case TypeError(): print("TypeError") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  24. asyncio.gather - return_exceptions ࢦఆ͋Γ $ python310 2_asyncio_gather_except_true.py async def main():

    """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") results=['੒ޭ', ValueError(), TypeError()] ValueError() for result in results: match result: case ValueError(): print("ValueError") case TypeError(): print("TypeError") TypeError() ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  25. asyncio.gather - return_exceptions ࢦఆ͋Γ $ python310 2_asyncio_gather_except_true.py async def main():

    """return_exceptions=True͋Γ""" results = await asyncio.gather( coro_success(), coro_value_err(), coro_type_err(), return_exceptions=True) print(f"{results=}") results=['੒ޭ', ValueError(), TypeError()] ValueError() for result in results: match result: case ValueError(): print("ValueError") case TypeError(): print("TypeError") TypeError() ݁ՌͷνΣοΫ͕ඞཁ ͢΂ͯͷλεΫͷ࣮ߦ͕ඞཁ ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ
  26. •ಉ࣌ʹ࣮ߦ͢Δඇಉظؔ਺Λ༻ҙ - asycn def Ͱఆٛ͢Δίϧʔνϯ 3ͭ༻ҙ ࣄલ४උ - มߋͳ͠ import

    asyncio async def coro_success(): return "੒ޭ" async def coro_value_err(): raise ValueError async def coro_type_err(): raise TypeError
  27. async def main(): asycio.TaskGroup 例外捕捉 async with asyncio.TaskGroup() as tg:

    task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()]
  28. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()]
  29. asycio.TaskGroup 例外捕捉 async def main(): async with asyncio.TaskGroup() as tg:

    task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") try:
  30. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}")
  31. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}") ಉظॲཧͷΑ͏ʹexceptͰัଊͰ͖Δ
  32. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}")
  33. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}") $ python311 3_asyncio_taskgroup_except.py
  34. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}") $ python311 3_asyncio_taskgroup_except.py err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) err=ExceptionGroup('unhandled errors in a TaskGroup', [TypeError()]) $ python311
  35. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}") $ python311 3_asyncio_taskgroup_except.py err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) err=ExceptionGroup('unhandled errors in a TaskGroup', [TypeError()]) $ python311 2ͭͷExceptionΛัଊͰ͖͍ͯΔ
  36. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}")
  37. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}")
  38. asycio.TaskGroup 例外捕捉 try: async def main(): async with asyncio.TaskGroup() as

    tg: task1 = tg.create_task(coro_success()) task2 = tg.create_task(coro_value_err()) task3 = tg.create_task(coro_type_err()) results = [task1.result(), task2.result(), task3.result()] except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}")
  39. asycio.TaskGroup 例外捕捉 except* ValueError as err: print(f"{err=}") except* TypeError as

    err: print(f"{err=}") except* except* err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) err=ExceptionGroup('unhandled errors in a TaskGroup', [TypeError()])
  40. 新構⽂ except* except* •PEP 654: Exception Groups and except*. •Python

    3.11ͷ৽ߏจ •૊ΈࠐΈͷྫ֎ܕʹ ExceptionGroup (3.11௥Ճ)ͱ߹Θͤͯ࢖͏
  41. 新構⽂ except* except* •PEP 654: Exception Groups and except*. •Python

    3.11ͷ৽ߏจ •૊ΈࠐΈͷྫ֎ܕʹ ExceptionGroup (3.11௥Ճ)ͱ߹Θͤͯ࢖͏ •ExceptionGroup ͱ͸ •ؔ࿈ੑͷͳ͍ෳ਺ͷྫ֎Λൃੜͤ͞Δඞཁ͕͋Δ৔߹ʹར༻͢Δ
  42. 新構⽂ except* except* •PEP 654: Exception Groups and except*. •Python

    3.11ͷ৽ߏจ •૊ΈࠐΈͷྫ֎ܕʹ ExceptionGroup (3.11௥Ճ)ͱ߹Θͤͯ࢖͏ •ExceptionGroup ͱ͸ •ؔ࿈ੑͷͳ͍ෳ਺ͷྫ֎Λൃੜͤ͞Δඞཁ͕͋Δ৔߹ʹར༻͢Δ •ExceptionGroup(“ϝοηʔδ”, [ValueError(1), TypeError(2)])
  43. 新構⽂ except* except* •PEP 654: Exception Groups and except*. •Python

    3.11ͷ৽ߏจ •૊ΈࠐΈͷྫ֎ܕʹ ExceptionGroup (3.11௥Ճ)ͱ߹Θͤͯ࢖͏ •ExceptionGroup •ؔ࿈ੑͷͳ͍ෳ਺ͷྫ֎Λൃੜͤ͞Δඞཁ͕͋Δ৔߹ʹར༻͢Δ •try except Ͱ΋ัଊͰ͖Δ •try except* Ͱัଊ͢Δͱෳ਺ͷexcept •ExceptionGroup(“ϝοηʔδ”, [ValueError(1), TypeError(2)]) ͱ͸
  44. asycio.TaskGroup IUUQTHJUIVCDPNQZUIPODQZUIPOCMPCNBJO-JCBTZODJPUBTLHSPVQTQZ class TaskGroup: ... async def __aexit__(self, et, exc,

    tb): ... if self._errors: # Exceptions are heavy objects that can have object # cycles (bad for GC); let's not keep a reference to # a bunch of them. errors = self._errors self._errors = None me = BaseExceptionGroup('unhandled errors in a TaskGroup', errors) raise me from None ExceptionGroup
  45. class TaskGroup: ... async def __aexit__(self, et, exc, tb): ...

    if self._errors: # Exceptions are heavy objects that can have object # cycles (bad for GC); let's not keep a reference to # a bunch of them. errors = self._errors self._errors = None me = BaseExceptionGroup('unhandled errors in a TaskGroup', errors) raise me from None IUUQTHJUIVCDPNQZUIPODQZUIPOCMPCNBJO-JCBTZODJPUBTLHSPVQTQZ asycio.TaskGroup ExceptionGroup
  46. IUUQTHJUIVCDPNQZUIPODQZUIPOCMPCNBJO-JCBTZODJPUBTLHSPVQTQZ class TaskGroup: ... async def __aexit__(self, et, exc, tb):

    ... if self._errors: # Exceptions are heavy objects that can have object # cycles (bad for GC); let's not keep a reference to # a bunch of them. errors = self._errors self._errors = None me = BaseExceptionGroup('unhandled errors in a TaskGroup', errors) raise me from None asycio.TaskGroup ExceptionGroup
  47. 新構⽂ except* except* •PEP 654: Exception Groups and except*. •PEP654ͷMotivationʢಈػʣʹ4ͭͷέʔε

    •Concurrent errorsʢಉ࣌ʹൃੜ͢ΔΤϥʔʣ ͳΜͷͨΊʹੜ·Εͨʁ •ෳ਺ͷλεΫ͕ૹग़͢Δྫ֎Λॲཧ͢Δྑ͍ํ๏͸ɺͳ͍ •asyncio.gather()ͷ՝୊
  48. 新構⽂ except* except* •PEP 654: Exception Groups and except*. •PEP654ͷMotivationʢಈػʣʹ4ͭͷέʔε

    •Concurrent errorsʢಉ࣌ʹൃੜ͢ΔΤϥʔʣ ͳΜͷͨΊʹੜ·Εͨʁ •ෳ਺ͷλεΫ͕ૹग़͢Δྫ֎Λॲཧ͢Δྑ͍ํ๏͸ɺͳ͍ •asyncio.gather()ͷ՝୊ •except* ߏจ͸ඇಉظॲཧͷͨΊʹ࣮૷͞Εͨɺͱݴͬͯ΋աݴͰ͸ͳ͍Α͏Ͱ͢
  49. 新構⽂ except* except* ͳΜͷͨΊʹੜ·Εͨʁ •except* ߏจ͸ඇಉظॲཧͷͨΊʹ࣮૷͞Εͨɺͱݴͬͯ΋աݴͰ͸ͳ͍Α͏Ͱ͢ •Python 3.11ͷ৽ػೳ(ͦͷ4ʣPEP 654 ྫ֎άϧʔϓͱTaskGroup

    •https://www.python.jp/news/wnpython311/except-grp.html •The 2021 Python Language Summit: PEP 654 — Exception Groups and except* •https://pyfound.blogspot.com/2021/05/the-2021-python-language-summit-pep-654.html • How Exception Groups Will Improve Error Handling in AsyncIO - Łukasz Langa | Power IT Conference •https://www.youtube.com/watch?v=Lfe2zsGS0Js
  50. •ඇಉظؔ਺Λ1ͭ௥Ճ - ͢͜͠଴ͭ ࣄલ४උ - 1ͭඇಉظؔ਺Λ௥Ճ import asyncio async def

    coro_success(): return "੒ޭ" async def coro_value_err(): raise ValueError async def coro_type_err(): raise TypeError มߋͳ͠ มߋͳ͠
  51. •ඇಉظؔ਺Λ1ͭ௥Ճ - ͢͜͠଴ͭ ࣄલ४උ - 1ͭඇಉظؔ਺Λ௥Ճ import asyncio async def

    coro_success(): return "੒ޭ" async def coro_value_err(): raise ValueError async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" มߋͳ͠ มߋͳ͠ ௥Ճ
  52. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main())
  53. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main())
  54. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main())
  55. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main())
  56. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py
  57. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ
  58. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢
  59. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ྫ֎͕ൃੜͨ͠৔߹ ଞͷλεΫ͸ಈ͍ͨ·· 
 ಈ࡞Λ༧ଌͮ͠Β͍
  60. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ɾࢭΊ͍ͨ ɾϩʔϧόοΫ͍ͨ͠ ྫ֎͕ൃੜͨ͠৔߹ ଞͷλεΫ͸ಈ͍ͨ·· 
 ಈ࡞Λ༧ଌͮ͠Β͍
  61. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ɾࢭΊ͍ͨ ɾϩʔϧόοΫ͍ͨ͠ ྫ֎͕ൃੜͨ͠৔߹ ଞͷλεΫ͸ಈ͍ͨ·· 
 ಈ࡞Λ༧ଌͮ͠Β͍
  62. ࣄલ४උ - Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ import asyncio async def coro_success(): return "੒ޭ"

    async def coro_value_err(): raise ValueError async def coro_long(): มߋͳ͠ มߋͳ͠ มߋ await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ
  63. async def coro_long(): มߋ await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" try:

    •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ ࣄલ४උ - Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ
  64. async def coro_long(): มߋ await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ ࣄલ४උ - Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ
  65. async def coro_long(): มߋ await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ ࣄલ४උ - Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ
  66. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: print(f"{results=}")

    except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) asyncio.run(main()) print("ऴྃ") Python 3.10Ҏલ - asyncio.gather()
  67. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: results

    = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") Python 3.10Ҏલ - asyncio.gather()
  68. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) λεΫͷঢ়ଶΛ ֬ೝ͠΍͘͢͢Δ ॲཧͱͯ͠͸ಉ͡ print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") Python 3.10Ҏલ - asyncio.gather()
  69. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") for task in [task1, task2, task3]: if task.done() is False: Task.done()Ͱ ະ׬ྃͷλεΫΛಛఆ Python 3.10Ҏલ - asyncio.gather()
  70. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ ະ׬ྃͷλεΫʹର͠ Task.cancel()Λ࣮ߦ print("ऴྃ") Python 3.10Ҏલ - asyncio.gather()
  71. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ ׬ྃ଴ͪͷ asyncio.sleep() print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather()
  72. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ શମ͜Μͳײ͡ print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather()
  73. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ͓͞Β͍ मਖ਼લͷ࣮ߦ
  74. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ͓͞Β͍ मਖ਼લͷ࣮ߦ ྫ֎ัଊ ऴ͕ྃग़ྗ
  75. λεΫ͕࢒ͬͯ͠·͏έʔε - asyncio.gather - return_exceptions ࢦఆͳ͠ async def main(): """return_exceptionsͳ͠"""

    try: results = await asyncio.gather( coro_success(), coro_value_err(), coro_long() ) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) print("ऴྃ") asyncio.run(main()) $ python310 4_remaining_tasks.py err=ValueError() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ͓͞Β͍ मਖ਼લͷ࣮ߦ ࢒ͬͯΔλεΫ͔Βͷग़ྗ
  76. async def coro_long(): มߋ await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ ࣄલ४උ - Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ
  77. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) ࣮ߦͯ͠Έ·͠ΐ͏ Python 3.10Ҏલ - asyncio.gather() async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError
  78. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError
  79. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py err=ValueError() async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError
  80. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py err=ValueError() Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError
  81. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py err=ValueError() Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError ऴྃ
  82. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Ωϟϯηϧ͞ΕΔͱ ඇಉظؔ਺ଆͰ ΩϟϯηϧΛัଊ Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py err=ValueError() Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError ऴྃ
  83. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Ωϟϯηϧ͞ΕΔͱ ඇಉظؔ਺ଆͰ ΩϟϯηϧΛัଊ Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py err=ValueError() Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError ऴྃ Ωϟϯηϧͷ͖΄Μ
  84. λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ async def main(): """return_exceptionsͳ͠""" try: task1

    = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) asyncio.run(main()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) Python 3.10Ҏલ - asyncio.gather() $ python310 5_canceling_task_gather.py err=ValueError() Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError ऴྃ λεΫͷঢ়ଶͷνΣοΫ Ωϟϯηϧॲཧͷ଴ͪ߹Θͤ
  85. async def main(): """return_exceptionsͳ͠""" try: task1 = asyncio.create_task(coro_success()) task2 =

    asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) asyncio.run(main()) print(f"{results=}") for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ results = await asyncio.gather(*[task1, task2, task3]) except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”)
  86. async def main(): """return_exceptionsͳ͠""" try: task1 = asyncio.create_task(coro_success()) task2 =

    asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) asyncio.run(main()) for task in [task1, task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ results = await asyncio.gather(*[task1, task2, task3]) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”)
  87. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: task1 = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) results = await asyncio.gather(*[task1, task2, task3]) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”)
  88. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: task1 = asyncio.create_task(coro_success()) task2 = asyncio.create_task(coro_value_err()) task3 = asyncio.create_task(coro_long()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”)
  89. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”)
  90. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long())
  91. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: print(f"{results=}") except ValueError as err: print(f"{err=}") except TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long())
  92. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long())
  93. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) for task in [task1,

    task2, task3]: if task.done() is False: task.cancel() # ະ׬ྃͷλεΫΛΩϟϯηϧ print("ऴྃ") await asyncio.sleep(1) asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏ async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long())
  94. async def main(): """return_exceptionsͳ͠""" try: asyncio.run(main()) print("ऴྃ") asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏

    async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long())
  95. async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") asycio.TaskGroup 処理 ϦϑΝΫλϦϯά͍͖ͯ͠·͠ΐ͏

    async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long())
  96. ࣮ߦͯ͠Έ·͠ΐ͏ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) asycio.TaskGroup 処理 $ python311 6_cancelled_task_taskgroup.py
  97. ࣮ߦͯ͠Έ·͠ΐ͏ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) asycio.TaskGroup 処理 $ python311 6_cancelled_task_taskgroup.py Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢
  98. ࣮ߦͯ͠Έ·͠ΐ͏ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) asycio.TaskGroup 処理 $ python311 6_cancelled_task_taskgroup.py err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢
  99. ࣮ߦͯ͠Έ·͠ΐ͏ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return "੒ޭʁ" except

    asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) asycio.TaskGroup 処理 $ python311 6_cancelled_task_taskgroup.py err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ ऴྃ
  100. ྫ֎ͷัଊͱ Ωϟϯηϧޙͷॲཧͷ ॱং͕ೖΕସΘ͍ͬͯΔ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return

    "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) asycio.TaskGroup 処理 $ python311 6_cancelled_task_taskgroup.py err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ ऴྃ
  101. TaskGroupͷதͰ ׬݁ ༧ଌ͠΍͍͢ಈ࡞ async def coro_long(): await asyncio.sleep(1) print("׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢") return

    "੒ޭʁ" except asyncio.CancelledError as err: try: print("Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢") raise asyncio.CancelledError async def main(): """TaskGroupͰͷΩϟϯηϧ""" try: asyncio.run(main()) print("ऴྃ") async with asyncio.TaskGroup() as g: print(f"{results=}") except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f”{err=}”) results = [task1.result(), task2.result(), task3.result()] task1 = g.create_task(coro_success()) task2 = g.create_task(coro_value_err()) task3 = g.create_task(coro_long()) asycio.TaskGroup 処理 $ python311 6_cancelled_task_taskgroup.py err=ExceptionGroup('unhandled errors in a TaskGroup', [ValueError()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ ऴྃ
  102. ৽͍͠ “Hello-ish world” import asyncio async def main(): async with

    asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...))
  103. ৽͍͠ “Hello-ish world” import asyncio async def main(): async with

    asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...)) try: except* BaseException as e: print(f"{e=}") asyncio.run(main())
  104. ৽͍͠ “Hello-ish world” import asyncio async def main(): async with

    asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...)) try: except* BaseException as e: print(f"{e=}") asyncio.run(main()) Python 3.11
  105. •Quickstart - Example 3-2. The “Hello-ish World” of Asyncio import

    asyncio import time async def main(): print(f"{time.ctime()} Hello!") await asyncio.sleep(1.0) print(f"{time.ctime()} GoodBye!") loop = asyncio.get_event_loop() task = loop.create_task(main()) loop.run_until_complete(task) pending = asyncio.all_tasks(loop=loop) for task in pending: task.cancel() group = asyncio.gather(*pending, return_exceptions=True) loop.run_until_complete(group) loop.close()
  106. •Quickstart - Example 3-2. import asyncio import time async def

    main(): print(f"{time.ctime()} Hello!") await asyncio.sleep(1.0) print(f"{time.ctime()} GoodBye!") The “Hello-ish World” of Asyncio λεΫͷঢ়ଶͷνΣοΫ Ωϟϯηϧॲཧͷ଴ͪ߹Θͤ loop = asyncio.get_event_loop() task = loop.create_task(main()) loop.run_until_complete(task) pending = asyncio.all_tasks(loop=loop) for task in pending: task.cancel() group = asyncio.gather(*pending, return_exceptions=True) loop.run_until_complete(group) loop.close()
  107. •Quickstart - Example 3-2. import asyncio import time async def

    coro(): print(f"{time.ctime()} Hello!") await asyncio.sleep(1.0) print(f"{time.ctime()} GoodBye!") The “Hello-ish World” of Asyncio λεΫͷঢ়ଶͷνΣοΫ Ωϟϯηϧॲཧͷ଴ͪ߹Θͤ async def main(): async with asyncio.TaskGroup() as tg: task = tg.create_task(coro(...)) try: except* BaseException as e: print(f"{e=}") asyncio.run(main())
  108. •λεΫͷঢ়ଶ ऄ଍ λεΫͷঢ়ଶ •PENDING - ࣮ߦ଴ͪʢ࣮ߦத΋͜Εʣ •FINISHED - ׬ྃ •CANCELLED

    - Ωϟϯηϧ •task._stateͰ֬ೝͰ͖Δ •αϯϓϧίʔυ͸ঢ়ଶ΋ग़ྗͯ͠·͢ɻͥͻಈ͔ͯ͠Έ͍ͯͩ͘͞ɻ ίʔυ͸ͪ͜Β IUUQTHJUIVCDPNKSGLUBML
  109. ऄ଍ TaskGroupͰHTTPX ίʔυ͸ͪ͜Β IUUQTHJUIVCDPNKSGLUBML import asyncio from time import time

    import httpx URL = "https://pokeapi.co/api/v2/pokemon/" async def access_url_poke(client, num: int) -> str: r = await client.get(f"{URL}{num}") pokemon = r.json() # JSONύʔε return pokemon["name"] # ϙέ໊Λൈ͘ async def main_poke(): """httpxͰϙέϞϯ151ඖҾͬ͜ൈ͘""" start = time() async with httpx.AsyncClient() as client: try: async with asyncio.TaskGroup() as tg: tasks = [ tg.create_task(access_url_poke(client, number)) for number in range(1, 151) ] except* BaseException as e: print(f"{e=}") results = [task.result() for task in tasks] print(results) print("time: ", time() - start) asyncio.run(main_poke())
  110. ऄ଍ ࣮͸·ͩ·ͩ͋Δ৽ػೳʂʁ ίʔυ͸ͪ͜Β IUUQTHJUIVCDPNKSGLUBML import asyncio async def main(): try:

    async with asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...)) except* BaseException as e: print(f"{e=}") with asyncio.Runner() as runner: runner.run(main()) 3.11 ৽ػೳ asyncio.Runner ͬͯͳʹʂʁ ΠϯλϥΫςΟϒηογϣϯ༻ʁʂ runΛෳ਺ճ࢖͑Δ
  111. 謝辞 •Using Asyncio in Python - Oreilly & Associates Inc

    •Python 3.11 Preview: Task and Exception Groups - realpython.com •https://realpython.com/python311-exception-groups/ •Python 3.11ͷ৽ػೳ(ͦͷ4ʣPEP 654 ྫ֎άϧʔϓͱTaskGroup •https://www.python.jp/news/wnpython311/except-grp.html •The 2021 Python Language Summit: PEP 654 — Exception Groups and except* •https://pyfound.blogspot.com/2021/05/the-2021-python-language-summit-pep-654.html • How Exception Groups Will Improve Error Handling in AsyncIO - Łukasz Langa | Power IT Conference •https://www.youtube.com/watch?v=Lfe2zsGS0Js