Created
October 24, 2018 19:54
-
-
Save unutbu/27131b75280ea7ed7618595c75b88867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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