Skip to content

Commit

Permalink
Merge pull request #113 from andfoy/env_var_reading_mode
Browse files Browse the repository at this point in the history
PR: Add PYWINPTY_BLOCK envirnoment variable to disable ptyprocess read block
  • Loading branch information
andfoy authored Jun 8, 2018
2 parents ca02693 + 81b12ea commit 0e05541
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions winpty/ptyprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, pty):
assert isinstance(pty, PTY)
self.pty = pty
self.pid = pty.pid
self.read_blocking = bool(os.environ.get('PYWINPTY_BLOCK', 1))
self.closed = False
self.flag_eof = False

Expand All @@ -51,7 +52,7 @@ def __init__(self, pty):

# Read from the pty in a thread.
self._thread = threading.Thread(target=_read_in_thread,
args=(address, self.pty))
args=(address, self.pty, self.read_blocking))
self._thread.setDaemon(True)
self._thread.start()

Expand Down Expand Up @@ -321,18 +322,18 @@ def setwinsize(self, rows, cols):
self.pty.set_size(cols, rows)


def _read_in_thread(address, pty):
def _read_in_thread(address, pty, blocking):
"""Read data from the pty in a thread.
"""
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(address)

while 1:
data = pty.read(4096, blocking=True)
data = pty.read(4096, blocking=blocking)

if not data and not pty.isalive():
while not data and not pty.iseof():
data += pty.read(4096, blocking=True)
data += pty.read(4096, blocking=blocking)

if not data:
try:
Expand Down

0 comments on commit 0e05541

Please sign in to comment.