from grep import coroutine from copipe import grep from cofollow import follow, printer @coroutine def broadcast(coros): while True: line = yield for coro in coros: coro.send(line) if __name__ == '__main__': f = open('access-log', 'r') p = printer() follow(f, broadcast((grep('python', p), grep('ply', p), grep('swig', p)))) f.close()
# -*- coding: utf-8 -*- """ multiple target Coroutines provide more powerful data routing possibilities than simple iterators If you built a collection of simple data processing components, you can glue them together into complex arrangements of pipes, branches, merging, etc. """ from coroutine import coroutine from cofollow import follow, grep, printer @coroutine def broadcast(targets): while True: line = (yield) if line is not None: for target in targets: target.send(line) if __name__ == "__main__": f = open("access-log") follow( f, broadcast([ grep('python', printer()), grep('ruby', printer()), grep('haskell', printer()) ]))
from coroutine import coroutine from cofollow import follow, printer @coroutine def grep(pattern, target): while True: line = (yield) if pattern in line: target.send(line) if __name__ == '__main__': f = open('www/access-log') follow(f, grep('python', printer()))
from coroutine import coroutine from cofollow import follow, printer from copipe import grep @coroutine def broadcast(targets): while True: item = (yield) for target in targets: target.send(item) if __name__ == '__main__': f = open('www/access-log') follow( f, broadcast([ grep('python', printer()), grep('ply', printer()), grep('swig', printer()), ]))