Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def tee(source, *sinks):
- qs = [Queue() for _ in sinks]
- def pull():
- p = Popen(source, stdout=PIPE, bufsize=1)
- for line in iter(p.stdout.readline, b''):
- for q in qs:
- q.put(line)
- q.put(None)
- p.stdout.close()
- p.wait()
- def push(sink, q):
- p = Popen(sink, stdin=PIPE, bufsize=1)
- while True:
- line = q.pop()
- if line is None:
- break
- p.stdin.write(line)
- p.stdin.close()
- p.wait()
- threads = [Thread(target=push, args=(sink, q)) for sink, q in zip(sinks, qs)]
- threads.append(Thread(target=pull))
- for thread in threads:
- thread.start()
- for thread in threads:
- thread.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement