Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 import subprocess
- 2 import time
- 3 import sys
- 4 import errno
- 5 import os
- 6 import fcntl
- 7 import signal
- 8
- 9 def unblock_stdout():
- 10 # get a non-blocking stdout
- 11 child_pid, child_pty = os.forkpty()
- 12 if child_pid != 0: # parent
- 13
- 14 # child is in its own process group, manually pass kill signals
- 15 signal.signal(signal.SIGINT, lambda signum, frame: os.kill(child_pid, signal.SIGINT))
- 16 signal.signal(signal.SIGTERM, lambda signum, frame: os.kill(child_pid, signal.SIGTERM))
- 17
- 18 fcntl.fcntl(sys.stdout, fcntl.F_SETFL,
- 19 fcntl.fcntl(sys.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
- 20
- 21 while True:
- 22 try:
- 23 dat = os.read(child_pty, 4096)
- 24 except OSError as e:
- 25 if e.errno == errno.EIO:
- 26 break
- 27 continue
- 28
- 29 if not dat:
- 30 break
- 31
- 32 try:
- 33 sys.stdout.write(dat.decode('utf8'))
- 34 except (OSError, IOError):
- 35 pass
- 36
- 37 os._exit(os.wait()[1])
- 38
- 39 unblock_stdout()
- 40 subprocess.Popen(["python", "run.py"],
- 41 preexec_fn=os.setpgrp)
- 42 while 1:
- 43 time.sleep(1)
- 44
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement