Python 扭曲贝壳:阻塞可执行文件

Python 扭曲贝壳:阻塞可执行文件,python,shell,twisted,deferred,twisted.conch,Python,Shell,Twisted,Deferred,Twisted.conch,我有一个工作程序,我正在尝试创建一个类似于“ping”的命令(命令类),每隔几秒钟打印一些数据 shell查找接收到的命令的可执行文件,用给定的参数实例化它们并运行它们: def lineReceived(self, lines): ... self.resume(None) def run_cmd_stack(self): cmd = self.cmd_stack.pop() exe = self.get_executable(cmd) exe.run

我有一个工作程序,我正在尝试创建一个类似于“ping”的命令(命令类),每隔几秒钟打印一些数据

shell查找接收到的命令的可执行文件,用给定的参数实例化它们并运行它们:

def lineReceived(self, lines):
    ...
    self.resume(None)

def run_cmd_stack(self):
    cmd = self.cmd_stack.pop()
    exe = self.get_executable(cmd)
    exe.run()

def resume(self, ret):
    """Used by Executables to signal their completion
    """
    self.RET = ret
    if len(self.cmd_stack) > 0:
        self.run_cmd_stack()
    else:
        self.showPrompt()
此代码有效,但感觉不正确:

class Executable(object):
    def __init__(self, shell_protocol, cmd, reactor):
        self.shell = shell_protocol
        self.reactor = reactor

    def end(self, ret):
        self.shell.resume(ret)


class exe_wait(Executable):
    name = 'wait'

    def run(self):
        i = int(self.args[0])
        self.loopy(i)
        return 0

    def loopy(self, i):
        d = defer.Deferred()
        if i > 0:
            self.shell.writeln("waiting...")
            d.addCallback(self.loopy)
            self.reactor.callLater(1, d.callback, i-1)
        else:
            d.addCallback(self.end)
            d.callback(0)
  • 我并不特别喜欢把反应堆作为一个论据来通过,这样我就可以用试验来测试它,但我不知道如何注入它只是为了测试
  • 我觉得让可执行文件调用shell的“resume()”方法很难看,我应该能够让exe在完成后“返回0”。我遇到的问题是shell只是运行并打印其提示符,而不是等待延迟的消息

  • 在我尝试过的所有方法中,这是唯一一种可以正常工作的方法,因此我非常感谢能得到的任何建议。

    我不太理解这里的问题-此示例无法运行,因此我无法检查可能的解决方案。我可以看出,对于第1点,不要害怕向事物传递参数:从长远来看,全球反应堆将被弃用,你需要将其作为参数传递到任何地方,所以现在就开始准备。我不太理解这里的问题-这个例子不可运行,所以我无法研究可能的解决方案。我可以看出,对于第1点,不要害怕向事物传递参数:从长远来看,全球反应堆将被弃用,你需要将其作为参数传递到任何地方,所以现在就开始准备。