Guest User

Untitled

a guest
Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import subprocess
  2. import time
  3. import os
  4. from threading import Thread
  5. from queue import Queue
  6.  
  7.  
  8. def enqueue_output(proc, queue):
  9. while not proc.poll():
  10. line = proc.stdout.readline()
  11. queue.put(line)
  12.  
  13.  
  14. def main(filepath):
  15. filepath = os.path.abspath(os.path.expanduser(filepath))
  16. proc = subprocess.Popen([filepath], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  17. queue = Queue()
  18. thread = Thread(target=enqueue_output, args=(proc, queue))
  19. thread.start()
  20.  
  21. while True:
  22. if queue.qsize():
  23. line = queue.get(timeout=.1).decode("utf-8").strip()
  24. print(line)
  25. else:
  26. time.sleep(.5)
  27.  
  28.  
  29. if __name__ == "__main__":
  30. main("sample.sh")
Add Comment
Please, Sign In to add comment