Skip to content

Instantly share code, notes, and snippets.

@unutbu
Created October 24, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unutbu/27131b75280ea7ed7618595c75b88867 to your computer and use it in GitHub Desktop.
Save unutbu/27131b75280ea7ed7618595c75b88867 to your computer and use it in GitHub Desktop.
import errno
import os
import pty
import select
from subprocess import Popen, STDOUT
master_fd, slave_fd = pty.openpty() # provide tty to enable
# line-buffering on ruby's side
proc = Popen(['python', 'test.py'],
stdin=slave_fd, stdout=slave_fd, stderr=STDOUT, close_fds=True)
os.close(slave_fd)
os.write(master_fd, b'abc\n')
timeout = .04 # seconds
try:
while 1:
ready, _, _ = select.select([master_fd], [], [], timeout)
if ready:
try:
data = os.read(master_fd, 512)
except OSError as e:
if e.errno != errno.EIO:
raise
break # EIO means EOF on some systems
else:
if not data: # EOF
break
print('got ' + repr(data))
finally:
os.close(master_fd)
if proc.poll() is None:
proc.kill()
proc.wait()
print("This is reached!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment