Slide 1

Slide 1 text

Python 3.11新機能 asyncio.TaskGroupと 2022年 asyncioの “Hello-ish world” Junya Fukuda PyCon JP 2022

Slide 2

Slide 2 text

•෱ా ൏໵ʢJunya Fukudaʣʢ@JunyaFffʣ •גࣜձࣾ೔ຊγεςϜٕݚʢJSLʣॴଐ ௕໺ݝͷձࣾ •WebΤϯδχΞ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ •ʢ͓͠͝ͱͰʣόϦϡʔϒοΫε •ຊ޷͖ʹѪ͞ΕΔαʔϏεΛ໨ࢦͯ͠ʢݹຊങऔɾൢചʣ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ ຊ೔ͷίʔυ͸ͪ͜Β

Slide 3

Slide 3 text

Python実践 •2022೥1݄ൃച ٕज़ධ࿦ࣾ •Python 3 ΤϯδχΞೝఆ࣮ફࢼݧ ओڭࡐ •2࡭໨ʹ͓͢͢Ίʂࣙॻతʹ࢖͍ͬͯ͋͛ͯͩ͘͞ʂ • ҎԼͷষΛ୲౰͍ͯ͠·͢ • Chapter2 ίʔσΟϯάن໿ • Chapter5 ܕώϯτ • Chapter19 ฒߦॲཧɼฒྻॲཧ

Slide 4

Slide 4 text

きょうはasyncioの新しい機能と 新しい “Hello-ish world”

Slide 5

Slide 5 text

asyncio、使っていますか?✋

Slide 6

Slide 6 text

ありがとうございます。

Slide 7

Slide 7 text

ですよね。 なぜ使わないのでしょうか?

Slide 8

Slide 8 text

- asyncio 基本 •ඇಉظॲཧͷίʔυΛॻͨ͘ΊͷެࣜϥΠϒϥϦ •ෳ਺ͷλεΫΛಉ࣌ʹॲཧ͢Δ •I/Oό΢ϯυͳॲཧʢDBΞΫηε/HTTPϦΫΤετͳͲʣͰޮՌΛൃش •֎෦API-A, ֎෦API-B, ֎෦API-C Λಉ࣌ʹ࣮ߦ͍ͨ͠ •ಉظॲཧͩͱ ॱ൪ʹ࣮ߦ " # $

Slide 9

Slide 9 text

- asyncio 基本 •ඇಉظॲཧͷίʔυΛॻͨ͘ΊͷެࣜϥΠϒϥϦ •ෳ਺ͷλεΫΛಉ࣌ʹॲཧ͢Δ •I/Oό΢ϯυͳॲཧʢDBΞΫηε/HTTPϦΫΤετͳͲʣͰޮՌΛൃش •֎෦API-A, ֎෦API-B, ֎෦API-C Λಉ࣌ʹ࣮ߦ͍ͨ͠ •ඇಉظॲཧͩͱ ·ͱΊͯಉ࣌ʹ࣮ߦ C B A

Slide 10

Slide 10 text

- asyncio 基本 •ඇಉظॲཧͷίʔυΛॻͨ͘ΊͷެࣜϥΠϒϥϦ •ຖόʔδϣϯਐԽ ߴϨϕϧAPI͕ॆ࣮ - ͱͯ΋ॻ͖΍͍͢ •ຊ΍PyCon JP 2021ͰͦΜͳτʔΫΛ͍͖ͤͯͨͩͨ͞·ͨ͠ɻͥͻʂ "4(*ΞϓϦέʔγϣϯೖ໳͜Θ͘ͳ͍BTZODJPجૅͱඇಉظ*0'BTU"1*Λྫʹ 1Z$PO+1 •ෳ਺ͷλεΫΛಉ࣌ʹॲཧ͢Δ •I/Oό΢ϯυͳॲཧʢDBΞΫηε/HTTPϦΫΤετͳͲʣͰޮՌΛൃش

Slide 11

Slide 11 text

🤔

Slide 12

Slide 12 text

asyncio 使 理由 考 •׳ΕͯΔಉظॲཧͰे෼ •IO͕ͳ͍ʢػցֶश΍σʔλ෼ੳ͕த৺ʣʢCPUʹ͕Μ͹ΒͤΔ͜ͱଟ͍ʣ •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ଞͷݴޠͰॻ͍ͪΌ͏ʢGo΍JS(nodejs/deno..),Rust,C#,SwiftͳͲʣ

Slide 13

Slide 13 text

asyncio 使 理由 考 •׳ΕͯΔಉظॲཧͰे෼ •IO͕ͳ͍ʢػցֶश΍σʔλ෼ੳ͕த৺ʣʢCPUʹ͕Μ͹ΒͤΔ͜ͱଟ͍ʣ •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ଞͷݴޠͰॻ͍ͪΌ͏ʢGo΍JS(nodejs/deno..),Rust,C#,SwiftͳͲʣ

Slide 14

Slide 14 text

asyncio 使 理由 考 •׳ΕͯΔಉظॲཧͰे෼ •IO͕ͳ͍ʢػցֶश΍σʔλ෼ੳ͕த৺ʣʢCPUʹ͕Μ͹ΒͤΔ͜ͱଟ͍ʣ •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ଞͷݴޠͰॻ͍ͪΌ͏ʢGo΍JS(nodejs/deno..),Rust,C#,SwiftͳͲʣ

Slide 15

Slide 15 text

asyncio 使 理由 考 •׳ΕͯΔಉظॲཧͰे෼ •IO͕ͳ͍ʢػցֶश΍σʔλ෼ੳ͕த৺ʣʢCPUʹ͕Μ͹ΒͤΔ͜ͱଟ͍ʣ •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ଞͷݴޠͰॻ͍ͪΌ͏ʢGo΍JS(nodejs/deno..),Rust,C#,SwiftͳͲʣ ݁ہཁ݅ʹΑΓͦ͏ Ͱ΋WebͰ͋Ε͹I/Oͷմ asyncioͰ࡞ͬͯ΋Α͍͔΋

Slide 16

Slide 16 text

asyncio 使 理由 考 •׳ΕͯΔಉظॲཧͰे෼ •IO͕ͳ͍ʢػցֶश΍σʔλ෼ੳ͕த৺ʣʢCPUʹ͕Μ͹ΒͤΔ͜ͱଟ͍ʣ •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ଞͷݴޠͰॻ͍ͪΌ͏ʢGo΍JS(nodejs/deno..),Rust,C#,SwiftͳͲʣ

Slide 17

Slide 17 text

asyncio 使 ? •͔֬ʹͦΕ͸ͦ͏ •JavaScriptͰͷίʔϧόοΫͱ͔ʢ͕ͦͬͪಈ͘Μ͔͍ͬʣ •Έͳ͞Μ΋ܦݧ͋Δ͔΋ʁ •͸ͨ·ͨྫ֎ىͬͨ࣌͜ɺͲ͏ͳΔΜͩΖ͏… •εϨου΍ϚϧνϓϩηεͰͷܦݧ •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏

Slide 18

Slide 18 text

「この処理、   例外起こるとどうなります?」

Slide 19

Slide 19 text

「この処理、   例外起こるとどうなります?」 ϨϏϡʔͰݴ͏͜ͱɾݴΘΕΔ͜ͱ͋Γ·ͤΜ͔ʁ

Slide 20

Slide 20 text

asyncio 使 ? •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •͔֬ʹͦΕ͸ͦ͏ •༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ྫ֎ىͬͨ࣌͜ɺͲ͏ͳΔͷ͔෼͔Βͳ͍ •ʮ͜ͷฒߦʹಈ͍ͯΔλεΫɺexceptͨ͠ΒͲ͏ͳΔͷʁʯ •ྫ֎ൃੜ࣌ɺଞͷλεΫ͸͍͍ͬͨ…

Slide 21

Slide 21 text

asyncio 使 ? •ඇಉظॲཧͬͯɺ༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •͔֬ʹͦΕ͸ͦ͏ •༧ଌͰ͖ͳ͍ಈ͖Λͦ͠͏ •ྫ֎ىͬͨ࣌͜ɺͲ͏ͳΔͷ͔෼͔Βͳ͍ •ʮ͜ͷฒߦʹಈ͍ͯΔλεΫɺexceptͨ͠ΒͲ͏ͳΔͷʁʯ •ྫ֎ൃੜ࣌ɺଞͷλεΫ͸͍͍ͬͨ… •͜ΕΛղܾͯ͘͠ΕΔ৽ػೳ

Slide 22

Slide 22 text

asyncio.TaskGroup

Slide 23

Slide 23 text

asyncio.TaskGroup 例外・キャンセル

Slide 24

Slide 24 text

本⽇

Slide 25

Slide 25 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ

Slide 26

Slide 26 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ •͍··ͰʢPython 3.10Ҏલʣͱ͜Ε͔ΒʢPython 3.11ʣΛͦΕͧΕ

Slide 27

Slide 27 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ •͍··ͰʢPython 3.10Ҏલʣͱ͜Ε͔ΒʢPython 3.11ʣΛͦΕͧΕ •ྫ֎͜Μͳʹ؆୯ʹॻ͚ΔΜͩʂ

Slide 28

Slide 28 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ •͍··ͰʢPython 3.10Ҏલʣͱ͜Ε͔ΒʢPython 3.11ʣΛͦΕͧΕ •ྫ֎͜Μͳʹ؆୯ʹॻ͚ΔΜͩʂ •Ωϟϯηϧͬͯ͜͏ͳͬͯΔΜͩ

Slide 29

Slide 29 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ •͍··ͰʢPython 3.10Ҏલʣͱ͜Ε͔ΒʢPython 3.11ʣΛͦΕͧΕ •ྫ֎͜Μͳʹ؆୯ʹॻ͚ΔΜͩʂ •Ωϟϯηϧͬͯ͜͏ͳͬͯΔΜͩ •asyncio.TaskGroup ࢼͯ͠ΈΑ͏ʙ

Slide 30

Slide 30 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ •͍··ͰʢPython 3.10Ҏલʣͱ͜Ε͔ΒʢPython 3.11ʣΛͦΕͧΕ •ྫ֎͜Μͳʹ؆୯ʹॻ͚ΔΜͩʂ •Ωϟϯηϧͬͯ͜͏ͳͬͯΔΜͩ •ʮ༧ଌՄೳʯͰʮ҆શʯͳasyncioͷॻ͖ํ •asyncio.TaskGroup ࢼͯ͠ΈΑ͏ʙ

Slide 31

Slide 31 text

本⽇ •asyncioΛར༻͢Δ্Ͱͷྫ֎ॲཧɾΩϟϯηϧ •͍··ͰʢPython 3.10Ҏલʣͱ͜Ε͔ΒʢPython 3.11ʣΛͦΕͧΕ •ྫ֎͜Μͳʹ؆୯ʹॻ͚ΔΜͩʂ •Ωϟϯηϧͬͯ͜͏ͳͬͯΔΜͩ •ʮ༧ଌՄೳʯͰʮ҆શʯͳasyncioͷॻ͖ํ •৽͍͠ఆ൪ͱͳΔ “Hello-ish world” •asyncio.TaskGroup ࢼͯ͠ΈΑ͏ʙ

Slide 32

Slide 32 text

( 話 ?) •େ͖͘2ͭ

Slide 33

Slide 33 text

( 話 ?) •େ͖͘2ͭ •Python Core Developer Yury SelivanovࢯͷπΠʔτʢasync/await ͷAuthorʣ •Taskgroup͕merge͞Εͨ࣌ʹڵฃͷπΠʔτΛ࿈౤͍ͯͨ͠ •υΩϡϝϯτಡΜͰ΋Α͘෼͔Βͳ͍ •ͳʹ͕ͦΜͳʹ͍͍ͷ͔͠Β…🤔

Slide 34

Slide 34 text

( 話 ?) •͓ੈ࿩ʹͳ͍ͬͯΔେ޷͖ͳຊʮUsing Asyncio in Pythonʯ •෼͔Βͳ͍ίʔυ͕͋ͬͨ •େ͖͘2ͭ •Quickstart - Example 3-2.

Slide 35

Slide 35 text

•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()

Slide 36

Slide 36 text

( 話 ?) •͓ੈ࿩ʹͳ͍ͬͯΔຊʮUsing Asyncio in Pythonʯ •෼͔Βͳ͍ίʔυ͕͋ͬͨ •େ͖͘2ͭ •Quickstart - Example 3-2.

Slide 37

Slide 37 text

( 話 ?) •͓ੈ࿩ʹͳ͍ͬͯΔຊʮUsing Asyncio in Pythonʯ •෼͔Βͳ͍ίʔυ͕͋ͬͨ •େ͖͘2ͭ •Quickstart - Example 3-2. The “Hello-ish World” of Asyncio

Slide 38

Slide 38 text

( 話 ?) •͓ੈ࿩ʹͳ͍ͬͯΔຊʮUsing Asyncio in Pythonʯ •෼͔Βͳ͍ίʔυ͕͋ͬͨ •େ͖͘2ͭ •Quickstart - Example 3-2. The “Hello-ish World” of Asyncio •͜ͷίʔυΛ asyncio.TaskGroupͰ࡮৽͍ͨ͠

Slide 39

Slide 39 text

話 ・話 •࿩͢͜ͱ •asyncioͰͷྫ֎ॲཧʢ৽ߏจ except*΋ʣ •λεΫͷΩϟϯηϧ •asyncioͷ৽͍͠ϋϩʔϫʔϧυ •࿩͞ͳ͍͜ͱ •asyncioͷجຊͷ͸ͳ͠ •ΞϓϦέʔγϣϯͰ۩ମྫ •ASGIͰͷ͸ͳ͠ ίʔυ͸ͪ͜Β IUUQTHJUIVCDPNKSGLUBML

Slide 40

Slide 40 text

注意! •Python 3.11͸·ͩϦϦʔε͞Ε͍ͯͳ͍ʂ •10݄3೔ϦϦʔε༧ఆͰ͕ͨ͠ɺݱࡏ10݄24೔༧ఆʹͳ͍ͬͯΔ •ಈ࡞֬ೝ͸Python 3.11.0rc2 IUUQTQFQTQZUIPOPSHQFQ •ͳ͍ͱ͸ࢥ͍·͕͢ɺasyncio.TaskGroup͕ເͷػೳʹՄೳੑ΋θϩͰ͸… •͜ͷτʔΫ͸ɺΈͳ͞·ͷ৺͔Βফ͑Δ࢓૊Έʹͳ͍ͬͯ·͢ɻ

Slide 41

Slide 41 text

asyncio.TaskGroup

Slide 42

Slide 42 text

asyncio.TaskGroup •asyncio.TaskGroupͷػೳ •ෳ਺ͷλεΫΛ·ͱΊͯಉ࣌ʹॲཧ͢Δ

Slide 43

Slide 43 text

asyncio.TaskGroup •asyncio.TaskGroupͷػೳ •ෳ਺ͷλεΫΛ·ͱΊͯಉ࣌ʹॲཧ͢Δ •Python 3.10Ҏલʹ΋ಉ༷ͷػೳ͕͋Δ •asyncio.gather()΍asyncio.wait() ͳͲ

Slide 44

Slide 44 text

asyncio.TaskGroup •asyncio.TaskGroupͷػೳ •ෳ਺ͷλεΫΛ·ͱΊͯಉ࣌ʹॲཧ͢Δ •Python 3.10Ҏલʹ΋ಉ༷ͷػೳ͕͋Δ •asyncio.gather()΍asyncio.wait() ͳͲ •·ͣ͸ެࣜυΩϡϝϯτ͔ΒݟͯΈ·͠ΐ͏

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

import asyncio async def main(): await asyncio.gather( some_coro(...), another_coro(...) ) print("Both tasks have completed now.") asycio.TaskGroup

Slide 47

Slide 47 text

•asyncio.TaskGroupͷಛ௃ •ෳ਺ͷλεΫΛ·ͱΊͯॲཧ͢Δ •Python 3.10Ҏલʹ΋͋Δ •asyncio.gather()΍asyncio.wait() •ͦΕΒͱҟͳΔɺେ͖ͳಛ௃͕2ͭ͋Δ asycio.TaskGroup •ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ

Slide 48

Slide 48 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ asycio.TaskGroup 特徴

Slide 49

Slide 49 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ asycio.TaskGroup 特徴

Slide 50

Slide 50 text

•ෳ਺ͷλεΫΛ·ͱΊͯॲཧ͢Δ ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •౰વɺྫ֎΋ಉ࣌ʹൃੜ͢Δ৔߹͕͋Δ •Python 3.10ҎલͰͲ͏΍ͬͯัଊ͢Δͷ͔ݟͯΈ·͠ΐ͏ •Python 3.7Ͱ௥Ճ͞ΕͨߴϨϕϧAPI asyncio.gather() Λར༻ •asyncio.gather() ʹ͸ྫ֎ʹؔ͢ΔΦϓγϣϯʮreturn_exceptionsʯ •return_exceptions ࢦఆͳ͠ •return_exceptions ࢦఆ͋Γ •ࢦఆͨ͠λεΫʢඇಉظؔ਺ʣΛಉ࣌ʹ࣮ߦ

Slide 51

Slide 51 text

•ಉ࣌ʹ࣮ߦ͢Δඇಉظؔ਺Λ༻ҙ - asycn def Ͱఆٛ͢Δίϧʔνϯ 3ͭ༻ҙ ࣄલ४උ

Slide 52

Slide 52 text

•ಉ࣌ʹ࣮ߦ͢Δඇಉظؔ਺Λ༻ҙ - asycn def Ͱఆٛ͢Δίϧʔνϯ 3ͭ༻ҙ ࣄલ४උ import asyncio async def coro_success(): return "੒ޭ" async def coro_value_err(): raise ValueError async def coro_type_err(): raise TypeError

Slide 53

Slide 53 text

•asyncio.gather - return_exceptions ࢦఆͳ͠ •asyncio.gather - return_exceptions ࢦఆ͋Γ

Slide 54

Slide 54 text

•asyncio.gather - return_exceptions ࢦఆͳ͠ •asyncio.gather - return_exceptions ࢦఆ͋Γ

Slide 55

Slide 55 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 56

Slide 56 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 57

Slide 57 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 58

Slide 58 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 59

Slide 59 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 60

Slide 60 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 61

Slide 61 text

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()) ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 62

Slide 62 text

•asyncio.gather - return_exceptions ࢦఆͳ͠ •asyncio.gather - return_exceptions ࢦఆ͋Γ

Slide 63

Slide 63 text

•asyncio.gather - return_exceptions ࢦఆͳ͠ •asyncio.gather - return_exceptions ࢦఆ͋Γ return_exceptions ࢦఆ͋Γͷ৔߹ɺ ͢΂ͯͷλεΫΛ࣮ߦͨ͠ޙʹ݁ՌΛϦετͰऔಘ͢Δ

Slide 64

Slide 64 text

•asyncio.gather - return_exceptions ࢦఆͳ͠ •asyncio.gather - return_exceptions ࢦఆ͋Γ return_exceptions ࢦఆ͋Γͷ৔߹ɺ ͢΂ͯͷλεΫΛ࣮ߦͨ͠ޙʹ݁ՌΛϦετͰऔಘ͢Δ ໭Γ஋ͷϦετΛνΣοΫ͢Δඞཁ͕͋Δ

Slide 65

Slide 65 text

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=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 66

Slide 66 text

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=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 67

Slide 67 text

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=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 68

Slide 68 text

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=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 69

Slide 69 text

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 ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 70

Slide 70 text

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 ͢΂ͯͷ݁ՌΛϦετͰऔಘ ͲΜͳྫ֎͕ൃੜ͔ͨ֬͠ೝ͕ඞཁ ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 71

Slide 71 text

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=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 72

Slide 72 text

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=}") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 73

Slide 73 text

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") ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 74

Slide 74 text

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() ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 75

Slide 75 text

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() ݁ՌͷνΣοΫ͕ඞཁ ͢΂ͯͷλεΫͷ࣮ߦ͕ඞཁ ಉ࣌ʹൃੜ͢Δྫ֎ͷัଊ

Slide 76

Slide 76 text

ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ Python 3.10Ҏલͷ՝୊ •asyncio.gather() ʹ͸ྫ֎ʹؔ͢ΔΦϓγϣϯʮreturn_exceptionsʯ •return_exceptions ࢦఆͳ͠ •return_exceptions ࢦఆ͋Γ

Slide 77

Slide 77 text

ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ Python 3.10Ҏલͷ՝୊ •࠷ॳͷྫ֎Ҏ֎ัଊͰ͖ͳ͍ •ൃੜͨ͠ྫ֎Λ͢΂ͯه࿥Ͱ͖ͳ͍ •໭Γ஋ͷϦετΛνΣοΫ͢Δඞཁ͕͋Δ •͢΂ͯͷλεΫΛ࣮ߦ͠ͳ͚Ε͹ͳΒͳ͍ •ͨͱ͑͹ɺ10ݸͷHTTPϦΫΤετΛಉ࣌ʹߦ͏ॲཧ •࠷ॳͷλεΫʹྫ֎͕ൃੜ •࢒Γͷࣦഊ͢Δ͔΋͠Εͳ͍9͕ͭ׬ྃ͢Δ·Ͱ଴ͭ͜ͱʹͳΔ ʢ݁Ռ10ݸ͢΂͕ͯಉ͡Τϥʔʹͳͬͨͱͯ͠΋Ͱ͢ʣ •return_exceptions ࢦఆͳ͠ •return_exceptions ࢦఆ͋Γ

Slide 78

Slide 78 text

ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ Python 3.10Ҏલͷ՝୊ •࠷ॳͷྫ֎Ҏ֎ัଊͰ͖ͳ͍ •ൃੜͨ͠ྫ֎Λ͢΂ͯه࿥Ͱ͖ͳ͍ •໭Γ஋ͷϦετΛνΣοΫ͢Δඞཁ͕͋Δ •͢΂ͯͷλεΫΛ࣮ߦ͠ͳ͚Ε͹ͳΒͳ͍ •return_exceptions ࢦఆͳ͠ •return_exceptions ࢦఆ͋Γ •ղܾ͢Δͷ͕ɺasyncio.TaskGroupͱ৽ߏจʮexcept*ʯ

Slide 79

Slide 79 text

•ಉ࣌ʹ࣮ߦ͢Δඇಉظؔ਺Λ༻ҙ - asycn def Ͱఆٛ͢Δίϧʔνϯ 3ͭ༻ҙ ࣄલ४උ - มߋͳ͠ import asyncio async def coro_success(): return "੒ޭ" async def coro_value_err(): raise ValueError async def coro_type_err(): raise TypeError

Slide 80

Slide 80 text

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()]

Slide 81

Slide 81 text

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()]

Slide 82

Slide 82 text

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:

Slide 83

Slide 83 text

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=}")

Slide 84

Slide 84 text

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ͰัଊͰ͖Δ

Slide 85

Slide 85 text

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=}")

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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ΛัଊͰ͖͍ͯΔ

Slide 89

Slide 89 text

asyncio.TaskGroupͱ৽ߏจʮexcept*ʯ •ಉ࣌ʹൃੜ͢Δྫ֎ΛัଊͰ͖Δ •ಉظॲཧͱಉ͡Α͏ͳॻ͖ํͰಡΈ΍͍͢ asyncio.TaskGroupͷ1ͭ໨ͷಛ௃Ͱ͢

Slide 90

Slide 90 text

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=}")

Slide 91

Slide 91 text

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=}")

Slide 92

Slide 92 text

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=}")

Slide 93

Slide 93 text

asycio.TaskGroup 例外捕捉 except* ValueError as err: print(f"{err=}") except* TypeError as err: print(f"{err=}") except* except*

Slide 94

Slide 94 text

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()])

Slide 95

Slide 95 text

新構⽂ except* except* •PEP 654: Exception Groups and except*. •Python 3.11ͷ৽ߏจ

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

新構⽂ 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)]) ͱ͸

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

新構⽂ except* except* ͳΜͷͨΊʹੜ·Εͨʁ

Slide 104

Slide 104 text

新構⽂ except* except* •PEP 654: Exception Groups and except*. •PEP654ͷMotivationʢಈػʣʹ4ͭͷέʔε ͳΜͷͨΊʹੜ·Εͨʁ

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

新構⽂ 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

Slide 108

Slide 108 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ asycio.TaskGroup 特徴 •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ

Slide 109

Slide 109 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ asycio.TaskGroup 特徴

Slide 110

Slide 110 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫͷΩϟϯηϧͬͯͲ͏΍Δͷʁ asycio.TaskGroup 特徴

Slide 111

Slide 111 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫͷΩϟϯηϧͬͯͲ͏΍Δͷʁ •Python 3.10Ҏલ - asyncio.gather() •Python 3.11 - asyncio.TaskGroup() asycio.TaskGroup 特徴

Slide 112

Slide 112 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ྫ֎͕ൃੜͨ͠৔߹ɺಉ࣌ʹ࣮ߦ͍ͯͨ͠࢒ΓͷλεΫ͸Ͳ͏ͳΔͷͰ͠ΐ͏͔ʁ

Slide 113

Slide 113 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ྫ֎͕ൃੜͨ͠৔߹ɺಉ࣌ʹ࣮ߦ͍ͯͨ͠࢒ΓͷλεΫ͸Ͳ͏ͳΔͷͰ͠ΐ͏͔ʁ •࢒ΓͷλεΫ͕ͦͷ··࣮ߦ͞Εͯ͠·͏έʔε͕͋Δ

Slide 114

Slide 114 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ྫ֎͕ൃੜͨ͠৔߹ɺಉ࣌ʹ࣮ߦ͍ͯͨ͠࢒ΓͷλεΫ͸Ͳ͏ͳΔͷͰ͠ΐ͏͔ʁ •࢒ΓͷλεΫ͕ͦͷ··࣮ߦ͞Εͯ͠·͏έʔε͕͋Δ •asyncio.gather()Λྫʹ࢒ͬͯ͠·͏έʔεΛΈͯΈ·͠ΐ͏

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

•ඇಉظؔ਺Λ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 "੒ޭʁ" มߋͳ͠ มߋͳ͠ ௥Ճ

Slide 117

Slide 117 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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())

Slide 118

Slide 118 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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())

Slide 119

Slide 119 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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())

Slide 120

Slide 120 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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())

Slide 121

Slide 121 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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

Slide 122

Slide 122 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ

Slide 123

Slide 123 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢

Slide 124

Slide 124 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ྫ֎͕ൃੜͨ͠৔߹ ଞͷλεΫ͸ಈ͍ͨ·· 
 ಈ࡞Λ༧ଌͮ͠Β͍

Slide 125

Slide 125 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ɾࢭΊ͍ͨ ɾϩʔϧόοΫ͍ͨ͠ ྫ֎͕ൃੜͨ͠৔߹ ଞͷλεΫ͸ಈ͍ͨ·· 
 ಈ࡞Λ༧ଌͮ͠Β͍

Slide 126

Slide 126 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ɾࢭΊ͍ͨ ɾϩʔϧόοΫ͍ͨ͠ ྫ֎͕ൃੜͨ͠৔߹ ଞͷλεΫ͸ಈ͍ͨ·· 
 ಈ࡞Λ༧ଌͮ͠Β͍

Slide 127

Slide 127 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ྫ֎͕ൃੜͨ͠৔߹ɺಉ࣌ʹ࣮ߦ͍ͯͨ͠࢒ΓͷλεΫ͸Ͳ͏ͳΔͷͰ͠ΐ͏͔ʁ •࢒ΓͷλεΫ͕ͦͷ··࣮ߦ͞Εͯ͠·͏έʔε͕͋Δ •λεΫΛΩϟϯηϧ͢Δ ʢΩϟϯηϧ͸ɺ3.11Ͱ௥Ճ͞Εͨ΋ͷͰ͸ͳ͍ʣ

Slide 128

Slide 128 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ྫ֎͕ൃੜͨ͠৔߹ɺಉ࣌ʹ࣮ߦ͍ͯͨ͠࢒ΓͷλεΫ͸Ͳ͏ͳΔͷͰ͠ΐ͏͔ʁ •࢒ΓͷλεΫ͕ͦͷ··࣮ߦ͞Εͯ͠·͏έʔε͕͋Δ •λεΫΛΩϟϯηϧ͢Δ ʢΩϟϯηϧ͸ɺ3.11Ͱ௥Ճ͞Εͨ΋ͷͰ͸ͳ͍ʣ •Ωϟϯηϧͷجຊ •3.10ҎલͰasyncio.gather()Λར༻ͨ͠৔߹ͷΩϟϯηϧ •λεΫ͕࢒ͬͯ͠·͏ॲཧΛमਖ਼͍͖ͯ͠·͠ΐ͏ ͢͜͠ίʔυ͕௕͘ͳΓ·͢ 🙏

Slide 129

Slide 129 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫ͕࢒ͬͯ͠·͏ॲཧΛमਖ਼͍͖ͯ͠·͠ΐ͏ •Ωϟϯηϧ͞ΕΔඇಉظؔ਺ʢcoro_long()ʣɿ •࢒ͬͯΔλεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ •Ωϟϯηϧޙॲཧͷ׬ྃΛ଴ͭ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ •मਖ਼ͷ؍఺͸3ͭ

Slide 130

Slide 130 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫ͕࢒ͬͯ͠·͏ॲཧΛमਖ਼͍͖ͯ͠·͠ΐ͏ •Ωϟϯηϧ͞ΕΔඇಉظؔ਺ʢcoro_long()ʣɿ •࢒ͬͯΔλεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ •Ωϟϯηϧޙॲཧͷ׬ྃΛ଴ͭ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ •मਖ਼ͷ؍఺͸3ͭ

Slide 131

Slide 131 text

•task.cancel() •λεΫʹΩϟϯηϧཁٻΛ͢Δ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ

Slide 132

Slide 132 text

•task.cancel() •λεΫʹΩϟϯηϧཁٻΛ͢Δ •Ωϟϯηϧ͞ΕͨλεΫ΁ɺasyncio.CancelledErrorྫ֎͕ૹग़͞ΕΔ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ

Slide 133

Slide 133 text

•task.cancel() •λεΫʹΩϟϯηϧཁٻΛ͢Δ •Ωϟϯηϧ͞ΕͨλεΫ΁ɺasyncio.CancelledErrorྫ֎͕ૹग़͞ΕΔ •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ

Slide 134

Slide 134 text

•task.cancel() •λεΫʹΩϟϯηϧཁٻΛ͢Δ •Ωϟϯηϧ͞ΕͨλεΫ΁ɺasyncio.CancelledErrorྫ֎͕ૹग़͞ΕΔ •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ

Slide 135

Slide 135 text

•task.cancel() •λεΫʹΩϟϯηϧཁٻΛ͢Δ •Ωϟϯηϧ͞ΕͨλεΫ΁ɺasyncio.CancelledErrorྫ֎͕ૹग़͞ΕΔ •ͦͷͨΊɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛड͚औΔඞཁ͕͋Δ •͞Βʹɺඇಉظؔ਺಺Ͱasyncio.CancelledErrorΛ࠶ૹग़͢Δ •ͪΐͬͱΑ͘Θ͔Βͳ͍Ͱ͢Ͷɻ࣮૷ͯ͠Έ·͠ΐ͏ɻ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ

Slide 136

Slide 136 text

ࣄલ४උ - Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ 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Λ࠶ૹग़͢Δ

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

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

Slide 141

Slide 141 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫ͕࢒ͬͯ͠·͏ॲཧΛमਖ਼͍͖ͯ͠·͠ΐ͏ •Ωϟϯηϧ͞ΕΔඇಉظؔ਺ʢcoro_long()ʣɿ •࢒ͬͯΔλεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ •Ωϟϯηϧޙॲཧͷ׬ྃΛ଴ͭ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ •मਖ਼ͷ؍఺͸3ͭ

Slide 142

Slide 142 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫ͕࢒ͬͯ͠·͏ॲཧΛमਖ਼͍͖ͯ͠·͠ΐ͏ •Ωϟϯηϧ͞ΕΔඇಉظؔ਺ʢcoro_long()ʣɿ •࢒ͬͯΔλεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ •Ωϟϯηϧޙॲཧͷ׬ྃΛ଴ͭ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ •मਖ਼ͷ؍఺͸3ͭ

Slide 143

Slide 143 text

•λεΫʹΩϟϯηϧཁٻΛ͢Δ - Task.cancel() ࢒ͬͯΔλεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ •λεΫͷ׬ྃΛ൑ఆ͢Δ - Task.done() •Ωϟϯηϧޙॲཧͷ׬ྃΛ଴ͭ - asyncio.sleep() •ίʔυΛमਖ਼͍͖ͯ͠·͠ΐ͏

Slide 144

Slide 144 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 145

Slide 145 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 146

Slide 146 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 147

Slide 147 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 148

Slide 148 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 149

Slide 149 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 150

Slide 150 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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()

Slide 151

Slide 151 text

λεΫ͕࢒ͬͯ͠·͏৔߹ͷग़ྗΛͪΐͬͱ͓͞Β͍

Slide 152

Slide 152 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ͓͞Β͍ मਖ਼લͷ࣮ߦ

Slide 153

Slide 153 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ͓͞Β͍ मਖ਼લͷ࣮ߦ ྫ֎ัଊ ऴ͕ྃग़ྗ

Slide 154

Slide 154 text

λεΫ͕࢒ͬͯ͠·͏έʔε - 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() ऴྃ ׬͍ྃͯ͠ͳ͍λεΫ͕ग़ྗ͍ͯ͠·͢ ͓͞Β͍ मਖ਼લͷ࣮ߦ ࢒ͬͯΔλεΫ͔Βͷग़ྗ

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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

Slide 157

Slide 157 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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

Slide 158

Slide 158 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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

Slide 159

Slide 159 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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

Slide 160

Slide 160 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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 ऴྃ

Slide 161

Slide 161 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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 ऴྃ

Slide 162

Slide 162 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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 ऴྃ Ωϟϯηϧͷ͖΄Μ

Slide 163

Slide 163 text

λεΫ͕࢒ͬͯ͠·͏έʔε → λεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ Λ௥Ճ 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 ऴྃ λεΫͷঢ়ଶͷνΣοΫ Ωϟϯηϧॲཧͷ଴ͪ߹Θͤ

Slide 164

Slide 164 text

ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •λεΫ͕࢒ͬͯ͠·͏ॲཧΛमਖ਼͍͖ͯ͠·͠ΐ͏ •Ωϟϯηϧ͞ΕΔඇಉظؔ਺ʢcoro_long()ʣɿ •࢒ͬͯΔλεΫͷ֬ೝͱΩϟϯηϧΛࢦࣔ •Ωϟϯηϧޙॲཧͷ׬ྃΛ଴ͭ Ωϟϯηϧ࣌ͷޙॲཧΛ௥Ճ •मਖ਼ͷ؍఺͸3ͭ

Slide 165

Slide 165 text

asycio.TaskGroup 処理 •ίϯςΩετϚωʔδϟʔΛൈ͚ΔλΠϛϯάͰɺ

Slide 166

Slide 166 text

asycio.TaskGroup 処理 •ίϯςΩετϚωʔδϟʔΛൈ͚ΔλΠϛϯάͰɺ •࢒ͬͨλεΫ͕Ωϟϯηϧ͞ΕΔ •Ωϟϯηϧॲཧͷ׬ྃΛ଴ͭ

Slide 167

Slide 167 text

asycio.TaskGroup 処理 •ίϯςΩετϚωʔδϟʔΛൈ͚ΔλΠϛϯάͰɺ •࢒ͬͨλεΫ͕Ωϟϯηϧ͞ΕΔ •Ωϟϯηϧॲཧͷ׬ྃΛ଴ͭ •Ҏ্ʂ

Slide 168

Slide 168 text

asycio.TaskGroup 処理 •ίϯςΩετϚωʔδϟʔΛൈ͚ΔλΠϛϯάͰɺ •࢒ͬͨλεΫ͕Ωϟϯηϧ͞ΕΔ •Ωϟϯηϧॲཧͷ׬ྃΛ଴ͭ •Ҏ্ʂ •ཁ͸ಛʹԿ΋͢Δඞཁ͸͋Γ·ͤΜɻΩϟϯηϧ͞Ε·͢ɻ

Slide 169

Slide 169 text

asycio.TaskGroup 処理 •ίϯςΩετϚωʔδϟʔΛൈ͚ΔλΠϛϯάͰɺ •࢒ͬͨλεΫ͕Ωϟϯηϧ͞ΕΔ •Ωϟϯηϧॲཧͷ׬ྃΛ଴ͭ •Ҏ্ʂ •ཁ͸ಛʹԿ΋͢Δඞཁ͸͋Γ·ͤΜɻΩϟϯηϧ͞Ε·͢ɻ ͖͞΄ͲͷίʔυΛϦϑΝΫλ͍͖ͯ͠·͠ΐ͏

Slide 170

Slide 170 text

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=}”)

Slide 171

Slide 171 text

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=}”)

Slide 172

Slide 172 text

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=}”)

Slide 173

Slide 173 text

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=}”)

Slide 174

Slide 174 text

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=}”)

Slide 175

Slide 175 text

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())

Slide 176

Slide 176 text

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())

Slide 177

Slide 177 text

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())

Slide 178

Slide 178 text

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())

Slide 179

Slide 179 text

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())

Slide 180

Slide 180 text

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())

Slide 181

Slide 181 text

࣮ߦͯ͠Έ·͠ΐ͏ 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

Slide 182

Slide 182 text

࣮ߦͯ͠Έ·͠ΐ͏ 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 Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢

Slide 183

Slide 183 text

࣮ߦͯ͠Έ·͠ΐ͏ 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()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢

Slide 184

Slide 184 text

࣮ߦͯ͠Έ·͠ΐ͏ 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()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ ऴྃ

Slide 185

Slide 185 text

ྫ֎ͷัଊͱ Ωϟϯηϧޙͷॲཧͷ ॱং͕ೖΕସΘ͍ͬͯΔ 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()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ ऴྃ

Slide 186

Slide 186 text

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()]) Ωϟϯηϧ͞ΕͨλεΫ͕ग़ྗ͍ͯ͠·͢ ऴྃ

Slide 187

Slide 187 text

•ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ asycio.TaskGroup 特徴 •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ

Slide 188

Slide 188 text

asycio.TaskGroup 特徴 •ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ैདྷͷΑ͏ͳॻ͖ํͰγϯϓϧʹྫ֎Λัଊ •except* ʹΑͬͯಉ࣌ʹൃੜ͢Δྫ֎Λ༨͢͜ͱͳ͘

Slide 189

Slide 189 text

asycio.TaskGroup 特徴 •ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ैདྷͷΑ͏ͳॻ͖ํͰγϯϓϧʹྫ֎Λัଊ •ࢦࣔ͢Δ͜ͱͳ͘Ωϟϯηϧ •except* ʹΑͬͯಉ࣌ʹൃੜ͢Δྫ֎Λ༨͢͜ͱͳ͘ •λεΫΛ࣮ߦ࣌ʹҙࣝ͢Δඞཁ͕ͳ͘ͳͬͨ

Slide 190

Slide 190 text

asycio.TaskGroup 特徴 •ಉ࣌ʹൃੜͨ͠ྫ֎ͷัଊ •ྫ֎ൃੜ࣌ͷλεΫͷΩϟϯηϧ •ैདྷͷΑ͏ͳॻ͖ํͰγϯϓϧʹྫ֎Λัଊ •ࢦࣔ͢Δ͜ͱͳ͘Ωϟϯηϧ •except* ʹΑͬͯಉ࣌ʹൃੜ͢Δྫ֎Λ༨͢͜ͱͳ͘ •λεΫΛ࣮ߦ࣌ʹҙࣝ͢Δඞཁ͕ͳ͘ͳͬͨ •ʮ༧ଌՄೳʯͰʮ҆શʯɺ͔ͭಡΈ΍͍͢asyncioͷॻ͖ํ •৽͍͠ “Hello-ish world”

Slide 191

Slide 191 text

৽͍͠ “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(...))

Slide 192

Slide 192 text

৽͍͠ “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())

Slide 193

Slide 193 text

৽͍͠ “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

Slide 194

Slide 194 text

•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()

Slide 195

Slide 195 text

•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()

Slide 196

Slide 196 text

•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())

Slide 197

Slide 197 text

No content

Slide 198

Slide 198 text

•asyncio͸֎෦I/OͷͨΊͷ΋ͷ •֎෦ʹґଘ͢ΔͨΊɺྫ֎΋ى͖΍͍͢

Slide 199

Slide 199 text

•asyncio.TaskGroupͷྫ֎ͷัଊͱΩϟϯηϧ •asyncio͸֎෦I/OͷͨΊͷ΋ͷ •֎෦ʹґଘ͢ΔͨΊɺྫ֎΋ى͖΍͍͢

Slide 200

Slide 200 text

•asyncio.TaskGroupͷྫ֎ͷัଊͱΩϟϯηϧ •asyncio͸֎෦I/OͷͨΊͷ΋ͷ •֎෦ʹґଘ͢ΔͨΊɺྫ֎΋ى͖΍͍͢ •ॻ͖΍ͯ͘͢ɺಡΈ΍͍͢

Slide 201

Slide 201 text

•asyncio.TaskGroupͷྫ֎ͷัଊͱΩϟϯηϧ •asyncio͸֎෦I/OͷͨΊͷ΋ͷ •֎෦ʹґଘ͢ΔͨΊɺྫ֎΋ى͖΍͍͢ •ॻ͖΍ͯ͘͢ɺಡΈ΍͍͢ •TaskGroup͞ΘͬͯΈΑ͏͔ͳ

Slide 202

Slide 202 text

•asyncio.TaskGroupͷྫ֎ͷัଊͱΩϟϯηϧ •asyncio͸֎෦I/OͷͨΊͷ΋ͷ •֎෦ʹґଘ͢ΔͨΊɺྫ֎΋ى͖΍͍͢ •ॻ͖΍ͯ͘͢ɺಡΈ΍͍͢ •TaskGroup͞ΘͬͯΈΑ͏͔ͳ •ྫ֎ɾΩϟϯηϧ͕͍··ͰͷίʔυΛݟ௚͖͔͚ͬ͢

Slide 203

Slide 203 text

•asyncio.TaskGroupͷྫ֎ͷัଊͱΩϟϯηϧ •asyncio͸֎෦I/OͷͨΊͷ΋ͷ •֎෦ʹґଘ͢ΔͨΊɺྫ֎΋ى͖΍͍͢ •ॻ͖΍ͯ͘͢ɺಡΈ΍͍͢ •TaskGroup͞ΘͬͯΈΑ͏͔ͳ •ྫ֎ɾΩϟϯηϧ͕͍··ͰͷίʔυΛݟ௚͖͔͚ͬ͢ •ΑΓշదͳasyncioϥΠϑΛ

Slide 204

Slide 204 text

•λεΫͷঢ়ଶ ऄ଍ λεΫͷঢ়ଶ •PENDING - ࣮ߦ଴ͪʢ࣮ߦத΋͜Εʣ •FINISHED - ׬ྃ •CANCELLED - Ωϟϯηϧ •task._stateͰ֬ೝͰ͖Δ •αϯϓϧίʔυ͸ঢ়ଶ΋ग़ྗͯ͠·͢ɻͥͻಈ͔ͯ͠Έ͍ͯͩ͘͞ɻ ίʔυ͸ͪ͜Β IUUQTHJUIVCDPNKSGLUBML

Slide 205

Slide 205 text

ऄ଍ 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())

Slide 206

Slide 206 text

ऄ଍ ࣮͸·ͩ·ͩ͋Δ৽ػೳʂʁ ίʔυ͸ͪ͜Β 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Λෳ਺ճ࢖͑Δ

Slide 207

Slide 207 text

༧ఆߘͩͬͨͱ͜Ζ 🙏 •asyncio.timeout() •ͬͦ͜Γ௥Ճ͞ΕͨPython 3.11 ৽ػೳ •wait_for ͷෆ۩߹Λड͚ͯੜ·Εͨʁʂ •Implement asyncio.timeout() context manager #90927 IUUQTHJUIVCDPNQZUIPODQZUIPOJTTVFT …

Slide 208

Slide 208 text

༧ఆߘͩͬͨͱ͜Ζ 🙏 •asyncioਐԽͷྺ࢙ •࣮͸Python1.5͔Β͋ͬͨඇಉظػೳ asyncore •αʔυύʔςΟͰͷඇಉظ࣮૷ •curio, trio , AnyIO, quattro ͳͲͳͲ … …

Slide 209

Slide 209 text

謝辞 •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

Slide 210

Slide 210 text

ご静聴ありがとうございました 👋