Advertisement
Guest User

Untitled

a guest
Oct 15th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. def tee(source, *sinks):
  2.     qs = [Queue() for _ in sinks]
  3.     def pull():
  4.         p = Popen(source, stdout=PIPE, bufsize=1)
  5.         for line in iter(p.stdout.readline, b''):
  6.             for q in qs:
  7.                 q.put(line)
  8.         q.put(None)
  9.         p.stdout.close()
  10.         p.wait()
  11.     def push(sink, q):
  12.         p = Popen(sink, stdin=PIPE, bufsize=1)
  13.         while True:
  14.             line = q.pop()
  15.             if line is None:
  16.                 break
  17.             p.stdin.write(line)
  18.         p.stdin.close()
  19.         p.wait()
  20.     threads = [Thread(target=push, args=(sink, q)) for sink, q in zip(sinks, qs)]
  21.     threads.append(Thread(target=pull))
  22.     for thread in threads:
  23.         thread.start()
  24.     for thread in threads:
  25.         thread.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement