python/coroutin2.py


def coro(nm, opt):
    c = yield f"start coroutine {nm} opt={opt}"
    while c != 'stop':
        print(f"coro while {c} ${type(c)} {isinstance(c, int)}")
        n = None
        if isinstance(c, int):
            for i in range(c):
                n = yield i
                if n != None:
                    print("break")
                    break
        c = (yield f"got {c}") if n == None else n
    print("coro stop")
    yield 'stop'
 
print("o 0")
c = coro("eins", "mitOpt")
print(next(c))
print(c.send("send1"))
print(c.send(3.14))
print(c.send(2))
print(next(c))
print(next(c))
print(c.send(314))
print(next(c))
print(next(c))
print(next(c))
print(next(c))
print(c.send('stop'))
try:
    for i in range(19):
        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)}")