python/coroutine.py


def coro():
    ix = 1
    print("coro 1")
    hello = yield ix, "Hello", "Du"
    print(f"coro 2, hello received {hello!r}")
    ix += 1
    yield ix, hello
    print("coro 3")
    ix += 1
    r = yield ix, 'hh22'
    print(f"coro 4 r={r!r}")
    while r != None:
        ix += 1
        r2 = yield ix, r
        print(f"coro 5 r={r!r}")
        r =r2
    ix += 1
    yield ix, "veryLast"
 
print("o 0")
c = coro()
print("o 1")
print(next(c))
print("o 2")
print("send", c.send("World"))
print("o 3")
print(next(c))
print("o4 send")
o = c.send("W4")
print(o)
print("o5 send", c.send("W5"))
print("o6")
try:
    while True:
        print("o7 while .... next(c)", next(c))
except BaseException as e:
        print(f"o8 except in while next without default: {e!r} of {type(e)}")
print("o9")
print("o10 after last with default", next(c, "defaultValueInArg2fNext"))
import dis
dis.dis(coro)