扭曲的Python + spawnProcess.从命令获取输出 [英] Twisted Python + spawnProcess. Getting output from a command

查看:568
本文介绍了扭曲的Python + spawnProcess.从命令获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Twisted Python服务器包装Minecraft服务器应用程序,该服务器具有RESTful API,用于获取当前连接的播放器列表. Twisted应用通过 reactor.spawnProcess()启动Minecraft服务器. ,然后通过 ProcessTransport ,它写入stdin.读取stdout和stdin由单独的 protocol.ProcessProtocol 课堂.

I'm working to wrap the Minecraft server application with a Twisted Python server that has a RESTful API for getting the list of currently connected players. The Twisted app starts the minecraft server via reactor.spawnProcess(), then communicates via a ProcessTransport, which writes to stdin. Reading stdout and stdin is handled by a separate protocol.ProcessProtocol class.

鉴于我想获得一个非常特定的命令("list"命令的结果,该命令将返回如下内容:

Given that I want to get the results of a very specific command (the 'list' command, which returns something like this:

[INFO] Connected players: blah, blah2

如果我能够在stdout中挑选一个玩家列表行,那么最好的方法是将其传递给RESTful API视图,该视图要求连接的玩家列表?请记住,我的 stdout阅读器无法直接交流与函数试图获取已连接播放器列表的功能.我可以解析stdout并标识要传送的播放器列表,但我不确定如何将其传送到Web API视图,该视图会将已连接的播放器列表发送到客户端,因为该视图和stdout阅读器不是直接相连的联系人.

If I am able to pick out a player list line in stdout, what is the best way to deliver this to the RESTful API view that is asking for a list of connected players? Keep in mind that my stdout reader can not directly communicate with the function that is trying to get the list of connected players. I can parse stdout and identify the player list for delivery, I'm just not sure how to deliver it to the Web API view that will send the connected player list to a client, since the view and the stdout reader aren't in direct contact.

我有几种技巧可以解决此问题,但是如果有人有想法,我宁愿以正确的方式"做到这一点.

I've got a few hacky possible ways to handle this, but would much rather do this the "right way" if anyone has ideas.

推荐答案

首先,永远不要调用writeSomeData.呼叫write.第二,拥有全局协议实例可能不是一个好主意,出于所有通常的原因,全局变量通常不是一个好主意.

First, do not call writeSomeData ever. Call write. Second, having a global protocol instance is probably a bad idea, for all the usual reasons globals are generally a bad idea.

第三,将方法添加到ProcessProtocol子类中,以获取所需的信息.该协议的工作是知道如何将抽象的动作(例如询问玩家列表")转换为字节序列以进行传输,以及如何将接收到的字节序列转换回抽象的动作(例如过程告诉我这些玩家已连接).

Third, add a method to the ProcessProtocol subclass for getting the information you want. The protocol's job is knowing how to turn abstract actions, like "ask for a list of players" into byte sequences to transmit and how to turn received byte sequences back into abstract actions like "the process told me these players are connected".

class NotchianProcessProtocol(protocol.ProcessProtocol):
    ...
    def listPlayers(self):
        self.transport.write("list")
        self._waiting.append(Deferred())
        return self._waiting[-1]

    def outReceived(self, bytes):
        outbuffer = self.outbuffer + bytes
        lines, leftover = parseLines(outbuffer)
        self.outbuffer = leftover

        for line in lines:
            if line.startswith('[INFO] Connected players: '):
                self._waiting.pop(0).callback(line)

现在,您的任何引用了已连接NotchianProcessProtocol的代码都可以在其上调用listPlayers并获取Deferred,此后不久将触发已连接的玩家信息.

Now any of your code which has a reference to a connected NotchianProcessProtocol can call listPlayers on it and get back a Deferred which will fire with connected player information shortly afterwards.

这篇关于扭曲的Python + spawnProcess.从命令获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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