Skip to content

Trio

async programming for humans and snake people.

Example

import trio
import cv2
import numpy as np


cap = cv2.VideoCapture(0)
frame = np.array([])

async def child1():
    while True:
        code, frame = cap.read()
        print("[1]Remove frame!!!")
        await trio.sleep(1)
        print("[1] Wait 1sec and next...")

async def child2():
    while True:
        print("[2] Send 1sec !!")
        await trio.sleep(1)
        print("[2] Recv 2sec !!")
        await trio.sleep(2)

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!")

trio.run(parent)

See also

Favorite stie