コード例 #1
0
ファイル: cobroadcast.py プロジェクト: danny-94/coroutines
from copipe import follow, printer, grep
# Broadcast to multiple targets
from coroutine import coroutine


@coroutine
def broadcast(targets):
    while True:
        item = (yield)
        for target in targets:
            target.send(item)


# This takes series of coroutines(targets) and
# sends received items to all of them
# Example use:
if __name__ == "__main__":
    f = open("input_file")
    follow(
        f,
        broadcast([
            grep('python', printer()),
            grep('perl', printer()),
            grep('java', printer())
        ]))
コード例 #2
0
ファイル: cobroadcast.py プロジェクト: cuiy0006/dabeaz
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()
コード例 #3
0
class GrepHandler:
    def __init__(self, pattern, target):
        self.pattern = pattern
        self.target = target

    def send(self, line):
        if self.pattern in line:
            self.target.send(line)


@coroutine
def null():
    while True:
        item = (yield)


line = 'python is nice'
p1 = grep('python', null())
p2 = GrepHandler('python', null())
'''Results:

➜  generators-2 git:(master) ✗ python -i benchmark.py
>>> from timeit import timeit
>>> timeit("p1.send(line)", "from __main__ import line,p1", number=10000000)
timeit("p1.send(line)", "from __main__ import line,p1", number=10000000)
1.9681993200000107
>>> timeit("p2.send(line)", "from __main__ import line,p2", number=10000000)
timeit("p2.send(line)", "from __main__ import line,p2", number=10000000)
2.5010494020000067
'''
コード例 #4
0
ファイル: cobroadcast2.py プロジェクト: xiez/python3-tweaks
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')
    p = printer()
    follow(f, broadcast([
        grep('python', p),
        grep('ply', p),
        grep('swig', p),
    ]))
コード例 #5
0
ファイル: cobroadcast.py プロジェクト: xiez/python3-tweaks
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()),
        ]))
コード例 #6
0
from cothread import threaded, threaded_safe
from cobroadcast import broadcast
from copipe import grep
from cofollow import printer

p = printer()

target = broadcast([
    threaded(grep('foo', p)),
    threaded(grep('bar', p)),
])

# target = broadcast([
#     threaded_safe(grep('foo', p)),
#     threaded_safe(grep('bar', p)),
# ])

for i in range(100):
    target.send('foo is nice - %d' % i)
    target.send('bar is bad - %d' % i)

del p
del target
コード例 #7
0
# A more disturbing variation of use of broadcast coroutine
from copipe import follow,grep,printer
from cobroadcast import broadcast
if __name__ == "__main__":
    f = open('input_file')
    p = printer()
    follow(f,broadcast([
        grep('python',p),
        grep('perl',p),
        grep('java',p)
        ]
))