在单线程Python脚本中有控制台 [英] Having a console in a single-threaded Python script

查看:82
本文介绍了在单线程Python脚本中有控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单线程脚本中有一个交互式控制台,其中有几个TCP连接已打开。这意味着我不能只有一个标准的输入阻塞线程。



有没有一个简单的方法来做到这一点?

解决方案

您可以将InteractiveConsole子类'code'模块)和
覆盖push()方法与一个wrapper重定向stdout / stderr
到一个StringIO实例,然后转发到基础
InteractiveConsole的push()方法。你的包装器可以返回一个2元组
(更多,结果)其中'more'表示InteractiveConsole是否期望
多个输入,'result'是任何InteractiveConsole.push()写入
StringIO实例。



这听起来比它更难。这里是一个基本的前提:

  import cs 
从cStringIO import StringIO
从代码import InteractiveConsole
from contextlib import contextmanager

__all__ = ['Interpreter']


@contextmanager
def std_redirector(stdin = sys.stdin,stdout = sys .stdin,stderr = sys.stderr):
暂时重定向stdin / stdout / stderr

tmp_fds = stdin,stdout,stderr
orig_fds = sys。 stdin,sys.stdout,sys.stderr
sys.stdin,sys.stdout,sys.stderr = tmp_fds
yield
sys.stdin,sys.stdout,sys.stderr = orig_fds


类Interpreter(InteractiveConsole):
远程友好的InteractiveConsole子类

此类的行为就像InteractiveConsole,除了它
返回所有输出作为字符串,而不是发送到stdout / stderr


banner =(Python%s \\\
%s\\\
%(sys.version, sys.platform)+
'输入帮助,版权,信用或许可证'
'更多信息。\\\
')

ps1 = getattr(sys,ps1,>>> )
ps2 = getattr(sys,ps2,...)


def __init __(self,locals = None):
InteractiveConsole。 __init __(self,locals = locals)
self.output = StringIO()
self.output = StringIO()

def push(self,command):
返回执行`command'的结果

此函数暂时重定向stdout / stderr,然后简单地将
转发给基类的push()方法。它返回一个2元组
(more,result)其中`more`是一个布尔值,表示
解释器是否期望更多的输入[类似于基类push()],而
` result`是捕获的输出(如果有)从运行`command`。


self.output.reset()
self.output.truncate()
with std_redirector(stdout = self.output,stderr = self。输出)
try:
more = InteractiveConsole.push(self,command)
result = self.output.getvalue()
except(SyntaxError,OverflowError):
pass
return more,result

查看这个完整的例子, UDP套接字:





启动两个控制台,并在一个中运行server.py ,client.py in the other。
在client.py中看到的内容应该与python的
常规的交互式解释器不可区分,即使所有命令都是
,评论。



当然,使用这样的套接字是非常不安全的,但它
说明了如何异步评估外部输入。你
应该能够适应你的情况,只要你信任
输入源。当有人键入时,事情变得有趣:

  while True:continue 
pre>

但是这又是另一个问题...: - )


I would like to have an interactive console in a single-threaded script that has several TCP connections open. This means I can't just have a standard input blocking the thread.

Is there an easy way to do this? Or should I just put the console in its own thread and be done with it?

解决方案

You can subclass InteractiveConsole (from the builtin 'code' module) and override the push() method with a wrapper that redirects stdout/stderr to a StringIO instance before forwarding to the base InteractiveConsole's push() method. Your wrapper can return a 2-tuple (more, result) where 'more' indicates whether InteractiveConsole expects more input, and 'result' is whatever InteractiveConsole.push() wrote to your StringIO instance.

It sounds harder than it is. Here's the basic premise:

import sys
from cStringIO import StringIO
from code import InteractiveConsole
from contextlib import contextmanager

__all__ = ['Interpreter']


@contextmanager
def std_redirector(stdin=sys.stdin, stdout=sys.stdin, stderr=sys.stderr):
    """Temporarily redirect stdin/stdout/stderr"""

    tmp_fds = stdin, stdout, stderr
    orig_fds = sys.stdin, sys.stdout, sys.stderr
    sys.stdin, sys.stdout, sys.stderr = tmp_fds
    yield
    sys.stdin, sys.stdout, sys.stderr = orig_fds


class Interpreter(InteractiveConsole):
    """Remote-friendly InteractiveConsole subclass

    This class behaves just like InteractiveConsole, except that it
    returns all output as a string rather than emitting to stdout/stderr

    """
    banner = ("Python %s\n%s\n" % (sys.version, sys.platform) +
              'Type "help", "copyright", "credits" or "license" '
              'for more information.\n')

    ps1 = getattr(sys, "ps1", ">>> ")
    ps2 = getattr(sys, "ps2", "... ")


    def __init__(self, locals=None):
        InteractiveConsole.__init__(self, locals=locals)
        self.output = StringIO()
        self.output = StringIO()

    def push(self, command):
        """Return the result of executing `command`

        This function temporarily redirects stdout/stderr and then simply
        forwards to the base class's push() method.  It returns a 2-tuple
        (more, result) where `more` is a boolean indicating whether the
        interpreter expects more input [similar to the base class push()], and
        `result` is the captured output (if any) from running `command`.

        """
        self.output.reset()
        self.output.truncate()
        with std_redirector(stdout=self.output, stderr=self.output):
            try:
                more = InteractiveConsole.push(self, command)
                result = self.output.getvalue()
            except (SyntaxError, OverflowError):
                pass
            return more, result

Check out this complete example, which accepts input from a UDP socket:

Start two consoles and run server.py in one, client.py in the other. What you see in client.py should be indistinguishable from python's regular interactive interpreter, even though all commands are being round-tripped to server.py for evaluation.

Of course, using sockets like this is terribly insecure, but it illustrates how to evaluate an external input asynchronously. You should be able to adapt it to your situation, as long as you trust the input source. Things get 'interesting' when someone types:

while True: continue

But that's another problem entirely... :-)

这篇关于在单线程Python脚本中有控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆