1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
# tasks-with-trace.py
import trio
async def child1():
print(" child1: started! sleeping now...")
await trio.sleep(1)
print(" child1: exiting!")
async def child2():
print(" child2 started! sleeping now...")
await trio.sleep(1)
print(" child2 exiting!")
async def parent():
print("parent: started!")
async with trio.open_nursery() as nursery:
print("parent: spawning child1...")
nursery.start_soon(child1)
print("parent: spawning child2...")
nursery.start_soon(child2)
print("parent: waiting for children to finish...")
# -- we exit the nursery block here --
print("parent: all done!")
class Tracer(trio.abc.Instrument):
def before_run(self):
print("!!! run started")
def _print_with_task(self, msg, task):
# repr(task) is perhaps more useful than task.name in general,
# but in context of a tutorial the extra noise is unhelpful.
print(f"{msg}: {task.name}")
def task_spawned(self, task):
self._print_with_task("### new task spawned", task)
def task_scheduled(self, task):
self._print_with_task("### task scheduled", task)
def before_task_step(self, task):
self._print_with_task(">>> about to run one step of task", task)
def after_task_step(self, task):
self._print_with_task("<<< task step finished", task)
def task_exited(self, task):
self._print_with_task("### task exited", task)
def before_io_wait(self, timeout):
if timeout:
print(f"### waiting for I/O for up to {timeout} seconds")
else:
print("### doing a quick check for I/O")
self._sleep_time = trio.current_time()
def after_io_wait(self, timeout):
duration = trio.current_time() - self._sleep_time
print(f"### finished I/O check (took {duration} seconds)")
def after_run(self):
print("!!! run finished")
trio.run(parent, instruments=[Tracer()])
|