【发布时间】:2019-08-27 12:54:32
【问题描述】:
我正在尝试编写一个需要在单独的线程上运行套接字连接的 shell。在我的测试中,当使用print() 而cmd.Cmd.cmdloop() 等待输入时,打印显示错误。
from core.shell import Shell
import time
import threading
def test(shell):
time.sleep(2)
shell.write('Doing test')
if __name__ == '__main__':
shell = Shell(None, None)
testThrd = threading.Thread(target=test, args=(shell,))
testThrd.start()
shell.cmdloop()
当上述命令运行时,会发生以下情况:
python test.py
Welcome to Test shell. Type help or ? to list commands.
>>asd
*** Unknown syntax: asd
>>[17:59:25] Doing test
如您所见,从另一个线程打印会在提示 >> 之后添加输出,而不是在新行中。我怎样才能让它出现在新行中并出现提示?
【问题讨论】:
-
我在
core.shell上找不到任何文档,但是如果您可以重定向Shell的输出,那么您可以很容易地实现管理标准输出...。这取决于什么该模块支持。 -
这是一个自定义创建的模块,继承
cmd.Cmd类。 docs.python.org/3/library/cmd.html
标签: python-3.x console-application windows-console