Streaming stdin / stdout in Python

I am trying to pass a bash shell to / from a simple WebSockets user interface, but I am having problems with IO redirection. I want to start a bash instance and connect the stdout and stdin functions to write_message () and on_message (), which interact with my web interface. Here is a simplified version of what I'm trying to do:

class Handler(WebSocketHandler): def open(self): print "New connection opened." self.app = subprocess.Popen(["/bin/bash", "--norc", "-i"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False) thread.start_new_thread(self.io_loop, ()) def on_message(self, message): self.app.stdin.write(message) def on_close(self): self.app.terminate() def io_loop(self): while self.app.poll() is None: line = self.app.stdout.readline() if line: self.write_message(line) 

While bash is starting and on_message is calling, I am not getting any output. readline () remains a lock. I tried stdout.read (), stdout.read (1) and various buffer modifications, but still do not output. I also tried hardcoding commands with a trailing "\ n" in on_message to isolate the problem, but I still don't get any output from readline ().

Ideally, I want to transfer every byte written to stdout in real time, without waiting for EOL or other characters, but it's hard for me to find the right API. Any pointers would be appreciated.

+4
source share
1 answer

It seems to me that this is a line:

 line = self.app.stdout.readline() 

will block the launch of ioloop, because the application will spend most of its time in readline() , waiting for the application to write some output. To make this work, you will need to get the stdin and stdout process (and what about stderr ?), You also need to capture it), switch them to non-blocking mode and add them to the set of file descriptors that ioloop spends the cycle time.

+3
source

Source: https://habr.com/ru/post/1381992/


All Articles