online compiler这是我的网站,用户可以在其中运行控制台程序。

目前,用户必须在运行程序之前输入程序输入。我正在尝试为程序建立实时用户输入(希望提供与他们在笔记本电脑上运行程序相同的体验)。

在研究中,我遇到了一个使用websocket流stdout和stdin的解决方案。

我的实施

# coding: utf-8
import subprocess
import thread

from tornado.websocket import WebSocketHandler

from nbstreamreader import NonBlockingStreamReader as NBSR


class WSHandler(WebSocketHandler):
    def open(self):
        self.write_message("connected")
        self.app = subprocess.Popen(['sh', 'app/shell.sh'], stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                                    shell=False)
        self.nbsr = NBSR(self.app.stdout)
        thread.start_new_thread(self.soutput, ())

    def on_message(self, incoming):
        self.app.stdin.write(incoming)

    def on_close(self):
        self.write_message("disconnected")

    def soutput(self):
        while True:
            output = self.nbsr.readline(0.1)
            # 0.1 secs to let the shell output the result
            if not output:
                print 'No more data'
                break
            self.write_message(output)


nbstreamreader.py

from threading import Thread
from Queue import Queue, Empty


class NonBlockingStreamReader:
    def __init__(self, stream):
        '''
        stream: the stream to read from.
                Usually a process' stdout or stderr.
        '''

        self._s = stream
        self._q = Queue()

        def _populateQueue(stream, queue):
            '''
            Collect lines from 'stream' and put them in 'quque'.
            '''

            while True:
                line = stream.readline()
                if line:
                    queue.put(line)
                else:
                    raise UnexpectedEndOfStream

        self._t = Thread(target=_populateQueue,
                         args=(self._s, self._q))
        self._t.daemon = True
        self._t.start()  # start collecting lines from the stream

    def readline(self, timeout=None):
        try:
            return self._q.get(block=timeout is not None,
                               timeout=timeout)
        except Empty:
            return None


class UnexpectedEndOfStream(Exception): pass


shell.sh

#!/usr/bin/env bash
echo "hello world"
echo "hello world"
read -p "Your first name: " fname
read -p "Your last name: " lname
echo "Hello $fname $lname ! I am learning how to create shell scripts"


此代码流stdout直到shell.sh代码到达read语句。

请指导我我在做什么错。为什么它不等待标准输入并在完成程序执行之前到达打印“ No more data”的信息?

测试它的源代码https://github.com/mryogesh/streamconsole.git

最佳答案

除非您在100毫秒内发送输入,否则您的readline()方法将超时,这将中断循环。您没有看到read -p提示符的原因是缓冲(由于readline和管道缓冲)。最后,您的示例javascript不会发送尾随换行符,因此read不会返回。

如果增加超时时间,请添加换行符和find a way to work around buffering issues,示例基本上应该可以正常工作。

我还将使用tornado.process和协程而不是子进程和线程:

from tornado import gen
from tornado.process import Subprocess
from tornado.ioloop import IOLoop
from tornado.iostream import StreamClosedError
from tornado.websocket import WebSocketHandler


class WSHandler(WebSocketHandler):
    def open(self):
        self.app = Subprocess(['script', '-q', 'sh', 'app/shell.sh'], stdout=Subprocess.STREAM, stdin=Subprocess.STREAM)
        IOLoop.current().spawn_callback(self.stream_output)

    def on_message(self, incoming):
        self.app.stdin.write(incoming.encode('utf-8'))

    @gen.coroutine
    def stream_output(self):
        try:
            while True:
                line = yield self.app.stdout.read_bytes(1000, partial=True)
                self.write_message(line.decode('utf-8'))
        except StreamClosedError:
            pass

关于python - 带有websocket的实时流stdout和stdin,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41431882/

10-12 12:47