python - LoopingCall AMP 命令

标签 python twisted asynchronous-messaging-protocol

为什么我在尝试实现调用 AMP 命令的 LoopingCall 函数时收到错误?

from twisted.protocols.amp import AMP
from twisted.python.log import startLogging, err
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
from sys import stdout

import commands

startLogging(stdout)


class MyAMP:

    def __init__(self, host, port):
        destination = TCP4ClientEndpoint(reactor, host, port)
        self.protocol = AMP()
        self.d = connectProtocol(destination, self.protocol)

    def say(self):
        return self.protocol.callRemote(commands.Say,
                                   phrase='Hello world')


def loop(myamp):
    myamp.say()


def main(host, port):
    myamp = MyAMP(host, port)
    lc = LoopingCall(loop, myamp=myamp)
    lc.start(4.0)
    reactor.run()

main('127.0.0.1', 12345)

在循环内调用myamp.say()时出错:

2013-08-16 12:28:58-0400 [-] Starting factory <twisted.internet.endpoints.OneShotFactory instance at 0x92273ec>
2013-08-16 12:28:58-0400 [-] Unhandled error in Deferred:
2013-08-16 12:28:58-0400 [-] Unhandled Error
    Traceback (most recent call last):
      File "lib/client.py", line 35, in <module>
        main('127.0.0.1', 12345)
      File "lib/client.py", line 32, in main
        lc.start(4.0)
      File "/usr/local/lib/python2.7/site-packages/twisted/internet/task.py", line 173, in start
        self()
      File "/usr/local/lib/python2.7/site-packages/twisted/internet/task.py", line 218, in __call__
        d = defer.maybeDeferred(self.f, *self.a, **self.kw)
    --- <exception caught here> ---
      File "/usr/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 137, in maybeDeferred
        result = f(*args, **kw)
      File "lib/client.py", line 26, in loop
        myamp.say()
      File "lib/client.py", line 22, in say
        phrase='Hello world')
      File "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 821, in callRemote
        return co._doCommand(self)
      File "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1778, in _doCommand
        self.requiresAnswer)
      File "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 752, in _sendBoxCommand
        box._sendTo(self.boxSender)
      File "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 577, in _sendTo
        proto.sendBox(self)
    exceptions.AttributeError: 'NoneType' object has no attribute 'sendBox'

2013-08-16 12:28:58-0400 [Uninitialized] AMP connection established (HOST:IPv4Address(TCP, '127.0.0.1', 50457) PEER:IPv4Address(TCP, '127.0.0.1', 12345))

最佳答案

您正在尝试在建立连接之前callRemote。默认情况下,LoopingCall 将在您启动它时立即运行其函数。不要执行 lc.start(4.0),而是执行 lc.start(4.0, now=False)。这将在第一次调用之前等待四秒钟。

关于python - LoopingCall AMP 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18264661/

相关文章:

python - Django 测试客户端不处理异常?

Python 日志记录出现错误 : Too many open files

python - 扭曲的 python - tac 还是 py?

macos - 为雪豹而扭曲?

python : Clear the buffer in Twisted Transport write

python - 应用程序在微服务架构中通信的最佳方式是什么

python - imaplib2 : imap. gmail.com 处理程序 BYE 响应:系统错误

python - 从 CSV 为 Google map 创建 KML 文件