Having a console in a single-threaded Python script(在单线程 Python 脚本中拥有一个控制台)
问题描述
我想在一个单线程脚本中有一个交互式控制台,它打开了多个 TCP 连接.这意味着我不能只让标准输入阻塞线程.
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?
推荐答案
您可以继承 InteractiveConsole(来自内置的代码"模块)和使用重定向 stdout/stderr 的包装器覆盖 push() 方法在转发到基础之前到 StringIO 实例InteractiveConsole 的 push() 方法.您的包装器可以返回一个 2 元组(更多,结果)其中更多"表示 InteractiveConsole 是否期望更多输入,结果"是 InteractiveConsole.push() 写入的任何内容你的 StringIO 实例.
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
%s
" % (sys.version, sys.platform) +
'Type "help", "copyright", "credits" or "license" '
'for more information.
')
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
查看这个完整的示例,它接受来自 UDP 套接字的输入:
Check out this complete example, which accepts input from a UDP socket:
- http://files.evadeflow.com/pyrrepl.zip
启动两个控制台,一个运行 server.py,另一个运行 client.py.您在 client.py 中看到的应该与 python 没有区别常规交互式解释器,即使所有命令都在往返于 server.py 进行评估.
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 脚本中拥有一个控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在单线程 Python 脚本中拥有一个控制台
基础教程推荐
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01