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

タスクって今どうなってるの?3.14の新機能 asyncio ps と pstree でasy...

Avatar for Junya Fukuda Junya Fukuda
September 27, 2025

タスクって今どうなってるの?3.14の新機能 asyncio ps と pstree でasyncioのデバッグを (PyCon JP 2025)

タスクって今どうなってるの?3.14の新機能 asyncio ps と pstree でasyncioのデバッグを (PyCon JP 2025)

Description
Python標準ライブラリである非同期プログラミング用の「asyncio」には、プロファイルの難しさという課題があります。何がどこに依存しているのか、どこにどれだけ負荷がかかっているのか、といった情報をゼロコストで把握することが、言語仕様上困難でした。ただでさえ難しい印象のある並行処理をさらに難しくしてしまう一因として、プロファイリングのしづらさがあると感じています。

この課題を解決する新機能が、Python 3.14で追加される「asyncio」の「 ps」「pstree」です。

2025年10月にリリース予定のPython 3.14では、新しいAPI「capture_call_graph」「print_task_call_graph」およびタスクの新属性「task_awaited_by」が追加され、asyncioの実行中タスクの状態を可視化できるようになります。これにより、余計な負荷をかけずに、より容易に内部状態を把握することが可能になります。

本トークでは、実際のコードを交えながら、asyncioを用いた柔軟な内部状態の可視化手法をご紹介します。

Session: https://2025.pycon.jp/ja/timetable/talk/9A8NPM
X: https://x.com/JunyaFff
linkedin: https://www.linkedin.com/in/junya-fukuda-4622a863/

Avatar for Junya Fukuda

Junya Fukuda

September 27, 2025
Tweet

More Decks by Junya Fukuda

Other Decks in Technology

Transcript

  1. •book •Python࣮ફϨγϐ •ΤΩεύʔτPythonϓϩάϥϛϯάվగ4൛ •Junya Fukudaʢ@JunyaFffʣ Photos 📷 post 👍 •Software

    Engineer at Groove X, inc. •Talk: PyConJP, PyConUS 2024, EuroPython 2023 •࿈ࡌ: Python Monthly Topics | gihyo.jp ͖ΐ͏ͷίʔυ 🔍GitHub jrfk talk
  2. asyncio ps / pstree 概要 •ಈ࡞Λ֬ೝ͢Δͷʹඞཁͳ΋ͷ •Python3.14rc3 •αϯϓϧίʔυ → →

    ެࣜ, uv, docker ຊ೔ͷίʔυ 🔍GitHub jrfk talk •·ͣ͸࢖͍ํͱग़ྗΛݟ͍͖ͯ·͠ΐ͏
  3. asyncio ps / pstree 概要 1. python Λ࣮ߦ 2. ϓϩηεIDΛ֬ೝ

    3. `python -m asyncio ps PID` •·ͣ͸࢖͍ํͱग़ྗΛݟ͍͖ͯ·͠ΐ͏
  4. asyncio ps / pstree 概要 •asyncio ps → •asyncio pstree

    → 🤔 🤔 •·ͣ͸࢖͍ํͱग़ྗΛݟ͍͖ͯ·͠ΐ͏ 使 方
  5. asyncio ps / pstree 概要 import asyncio async def main():

    await asyncio.sleep(500) asyncio.run(main())
  6. asyncio ps / pstree 概要 •asyncio ps → •asyncio pstree

    → 🤔 🤔 •·ͣ͸࢖͍ํͱग़ྗΛݟ͍͖ͯ·͠ΐ͏ 使 方
  7. asyncio ps / pstree 概要 •asyncio ps → •asyncio pstree

    → දܗࣜͰtaskͷঢ়ଶΛग़ྗ 🤔 •·ͣ͸࢖͍ํͱग़ྗΛݟ͍͖ͯ·͠ΐ͏ 使 方
  8. asyncio ps / pstree 概要 •·ͣ͸࢖͍ํΛݟ͍͖ͯ·͠ΐ͏ •asyncio ps → •asyncio

    pstree → දܗࣜͰtaskͷঢ়ଶΛग़ྗ πϦʔߏ଄Ͱtaskͷঢ়ଶΛग़ྗ 使 方
  9. asyncio ps / pstree 概要 •࣮ߦํ๏ •asyncio ps → •asyncio

    pstree → දܗࣜͰtaskͷঢ়ଶΛग़ྗ πϦʔߏ଄Ͱtaskͷঢ়ଶΛग़ྗ 1. python Λ࣮ߦ 2. ϓϩηεIDΛ֬ೝ 3. `python -m asyncio ps PID` 使 方
  10. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  11. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  12. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  13. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  14. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  15. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  16. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  17. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  18. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  19. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  20. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  21. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  22. ද΍πϦʔͷݟํ tid task id task name coroutine stack awaiter chain

    awaiter name awaiter id Task-1 sleep -> restaurant 0x0 2897749 0x104e78230 2897749 0x104e78230 customer1.0 sleep -> cooking -> c sleep -> restaurant Task-1 0x104e78230 1行目 2行目 見出し
  23. •asyncio ps •asyncio pstree ද΍πϦʔͷݟํͷΰʔϧ •ʮcoroutine stack ʯ͕ࣗ෼λεΫͷঢ়ଶ •ʮawaiter ʯ׬ྃΛ଴͍ͬͯΔผλεΫͷঢ়ଶ

    •ʮcoroutine stack ʯʮawaiterʯ͕πϦʔঢ়ʹ •ʮԿΛͲ͏Έ͍͔ͨʯʹΑͬͯ࢖͍෼͚Δͷ͕ྑͦ͞͏
  24. •͞·͟·ͳasyncioͷϓϩϑΝΠϧͷྫ •asyncio ps / pstree ֓ཁ •asyncio ps / pstree

    ͷ۩ମతͳ࢖͍ํͷՄೳੑ •͖΄Μͷ࢖͍ํ •ද΍πϦʔͷݟํ
  25. •͞·͟·ͳasyncioͷϓϩϑΝΠϧͷྫ •asyncio ps / pstree ֓ཁ •asyncio ps / pstree

    ͷ۩ମతͳ࢖͍ํͷՄೳੑ •͖΄Μͷ࢖͍ํ •ද΍πϦʔͷݟํ