| 1 |
# tasks-intro.py |
| 2 |
|
| 3 |
import trio |
| 4 |
|
| 5 |
|
| 6 |
async def child1(): |
| 7 |
print(" child1: started! sleeping now...") |
| 8 |
await trio.sleep(1) |
| 9 |
print(" child1: exiting!") |
| 10 |
|
| 11 |
|
| 12 |
async def child2(): |
| 13 |
print(" child2: started! sleeping now...") |
| 14 |
await trio.sleep(1) |
| 15 |
print(" child2: exiting!") |
| 16 |
|
| 17 |
|
| 18 |
async def parent(): |
| 19 |
print("parent: started!") |
| 20 |
async with trio.open_nursery() as nursery: |
| 21 |
print("parent: spawning child1...") |
| 22 |
nursery.start_soon(child1) |
| 23 |
|
| 24 |
print("parent: spawning child2...") |
| 25 |
nursery.start_soon(child2) |
| 26 |
|
| 27 |
print("parent: waiting for children to finish...") |
| 28 |
# -- we exit the nursery block here -- |
| 29 |
print("parent: all done!") |
| 30 |
|
| 31 |
|
| 32 |
trio.run(parent) |