Skip to content

Commit

Permalink
Note "How to structure your program"
Browse files Browse the repository at this point in the history
  • Loading branch information
gottadiveintopython committed Nov 4, 2024
1 parent 3749e8b commit b161f3e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sphinx/notes-ja.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,28 @@ async操作が禁じられている場所
...
async for __ in async_iterator: # 駄目
...
-------------------
プログラムの構造
-------------------

理想を言えばプログラムは一つだけ"root"タスクを持ち、他のタスクはその子や子孫であるべきです。
そうする事でタスクの集合が一本の木で表され、プログラムの構造が明確になるからです。

asynckivyでそれを実現するには :func:`asynckivy.start` の呼び出しを一度だけにし、他のタスクは全て
:external+asyncgui:doc:`structured-concurrency-ja` を通して作ります。

.. code-block::
import asynckivy as ak
class YourApp(App):
def on_start(self):
self._root_task = ak.start(self.main())
def on_stop(self):
self._root_task.cancel()
async def main(self):
...
25 changes: 25 additions & 0 deletions sphinx/notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,28 @@ Here is a list of them:
...
async for __ in async_iterator: # NOT ALLOWED
...
-------------------------------
How to structure your program
-------------------------------

Ultimately, your program should have only one "root" task, with all other tasks as its children or descendants.
This is something that Trio forces you to do but asynckivy does not.

You can achieve this by calling :func:`asynckivy.start` only once in your program,
and spawning all other tasks through the :external+asyncgui:doc:`structured-concurrency` APIs.

.. code-block::
import asynckivy as ak
class YourApp(App):
def on_start(self):
self._root_task = ak.start(self.main())
def on_stop(self):
self._root_task.cancel()
async def main(self):
...

0 comments on commit b161f3e

Please sign in to comment.