Skip to content

Instantly share code, notes, and snippets.

@zengxs
Created December 4, 2017 10:25
Show Gist options
  • Save zengxs/7af74fe889a33522a79bc6e16ab6b9d9 to your computer and use it in GitHub Desktop.
Save zengxs/7af74fe889a33522a79bc6e16ab6b9d9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import threading
from queue import Queue
tailq = Queue(maxsize=10)
def tail(filename):
p = subprocess.Popen(
['tail', '-f', filename], # args
stdout=subprocess.PIPE,
)
while True:
line = p.stdout.readline()
tailq.put(line)
if not line:
break
if __name__ == '__main__':
threading.Thread(target=tail, args=('sample.txt',)).start()
while True:
print(tailq.get()) # block
# print(tailq.get_nowait()) # non-block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment