1

我有一个HistoricRecvLine用作twisted.internet.protocol.ProcessProtocol类的接口。我可以毫无问题地从协议获取信息,HistoricRecvLine但我似乎无法找到另一种方式的魔术绑定:也就是我希望协议中的信息显示在 CLI 中。更清楚地说,我想让 ProcessProtocol 创建的子进程的 stdout/stderr 消息出现在我的 HistoricRecvLine 的 CLI 实现中。

任何人都知道如何做到这一点,或者可以指出我正确的方向吗?

4

1 回答 1

0

Jean Paul Calderone在他的评论中提供了一个解决方案的提示。

命令行有对协议的引用。在协议中,创建一个方法set_cli_write_func(self, func)来设置正确的 cli 写入方法。当您构建 cli 时,请使用以下内容在适当的协议上调用该函数:

def lpr(self, message): 
    """Print a message to the screen.

    :type message: String
    :param message: The message string.
    """
    self.terminal.nextLine()
    self.terminal.write(_format_message(message))
    self.terminal.nextLine()
    self.drawInputLine()

所以这个方法被称为协议。

请注意,这会创建一种循环依赖,因此您需要在对象被销毁时检查它。

于 2013-05-21T07:21:18.413 回答