Open sentriz opened 5 years ago
Pexpect has a read_nonblocking
method:
gevent's tp_read
also works:
term.pty = PtyProcessUnicode.spawn(term.cmd, env=env, cwd=opts.get("cwd", None))
and then
def from_pty(wsock_xtermjs_observer):
while True:
b = gevent_os.tp_read(term.pty.fd, 65536).decode("utf-8")
wsock_xtermjs_observer.on_next(["stdout", b])
works like a charm, i.e. does not block other greenlets with gevent_os
= gevent.os
.
Other more manual method would be using gevent.select
, i.e. before read you check:
readable, _, _ = select.select([pty.fd], [], [], timeout)
PS: No silly question, my kids did not get their fairytale yesterday evening, because of this :-/
@AXGKl wow, that works very nicely. thank you very much. :)
one thing though: how are you detecting when the process has ended / there is no data left, so you can break the while True
?
from the docs "If end-of-file is reached, an empty string is returned.", and based on that, the following is not working:
while True:
# proc is an instance of PtyProcessUnicode
data = gevent_os.tp_read(proc.fd, 65536)
if not data:
break
use(data)
because I get a OSError: [Errno 5] Input/output error
on my second line when the program has quit (I think because the file desriptor is closed) so we never reach the if not data
I've tried
while not proc.closed:
data = gevent_os.tp_read(proc.fd, 65536)
use(data)
but it's the same issue
thank you very much
I'm running ptyprocess in read in a greenlet coroutine. so when I .read() and there's no data, it blocks and stops all the other greenlets. is it possible to read without blocking, or at least check if there's any new data?
Sorry it this is a silly question
Thank you