def consumer():
    r = ''
    while True:
        n = yield r
        if not n:
            return
        print('CONSUMER:%s' % n)
        r = '200OK'


def produce(c):
    c.send(None)
    n = 0
    while n < 5:
        n = n + 1
        print('PRODUCE:%s' % n)
        r = c.send(n)
        print('PRODUCE: consumer return:%s' % r)
    c.close()


# c = consumer()
# produce(c)
def accumulate():
    tally = 0
    while 1:
        next = yield
        if next is None:
            return tally
        tally += next

def gather_tallies(tallies):
    while 1:
        tally = yield from accumulate()
        print(tally)
        tallies.append(tally)

tallies = []
acc = gather_tallies(tallies)
next(acc)

for i in range(4):
    acc.send(i)
acc.send(None)
print(tallies)