简体   繁体   English

python / twisted上的客户端/服务器

[英]Client/server on python-twisted

I am trying to make a Twisted application that is an XMLRPC server on the one hand and a client of special asymmetric binary protocol on the other (let's call it "binary") Now it works like this: 我正在尝试制作一个Twisted应用程序,该应用程序一方面是XMLRPC服务器,另一方面是特殊非对称二进制协议的客户端(我们称其为“ binary”),现在它的工作方式如下:

  1. Receive xmlrpc request and return request id (I am server) 接收xmlrpc请求并返回请求ID(我是服务器)
  2. Make request to the "binary" server (I am client) 向“二进制”服务器发出请求(我是客户端)
  3. xmlrpc client makes polling requests with request id that was given in (1). xmlrpc客户端使用(1)中给出的请求ID进行轮询请求。
  4. Poll request returns either "try again later" or real result, if it is already given by "binary" protocol. 轮询请求返回“稍后重试”或实际结果(如果已由“二进制”协议给出)。

Now it works, but I want to discard polling procedures. 现在可以使用,但是我想放弃轮询程序。

But while I am inside xmlrpc request processing procedure, "binary" protocol exchange doesn't perform, so I never get a result from "binary" server. 但是,当我处于xmlrpc请求处理过程中时,“二进制”协议交换不会执行,因此我从“二进制”服务器中从未得到结果。

What can you suggest? 你有什么建议? Separate threads? 线程分开? Something else? 还有吗

from twisted.internet import reactor

reactor.connectTCP(globalconf.cfgBinServerAddr, globalconf.cfgBinServerPort, BinFactory(binProtocol))

reactor.listenTCP(globalconf.xmlrpcPort, server.Site(xmlrpcProtocol))
reactor.run()
  1. Receive xmlrpc request. 接收xmlrpc请求。 Compute request id if you need it to make request to the "binary" server. 如果需要它向“二进制”服务器发出请求,则计算请求ID。 Do not return from the method 不要从方法中返回
  2. Make asynchronous request to the "binary" server (you are client) in the same method. 使用相同的方法向“二进制”服务器(您是客户端)发出异步请求。 Do not return yet 还没回来
  3. Drop this step (the xmlrpc request is not complete yet, nothing to poll) 删除此步骤(xmlrpc请求尚未完成,没有可轮询的内容)
  4. Return a deferred "real result". 返回延迟的“实际结果”。 You are done. 大功告成 xmlrpc machinery will return real result when it is ready to the xmlrpc client 当准备好xmlrpc客户端时,xmlrpc机械将返回真实结果

Here's an example xmlrpc server that returns a deferred subprocess output: 这是一个示例xmlrpc服务器,它返回延迟的子进程输出:

#!/usr/bin/env python
from timeit import default_timer as timer
from twisted.internet import reactor, utils
from twisted.web import xmlrpc, server

class Example(xmlrpc.XMLRPC):
    def xmlrpc_echo(self, x):
        """Sanity check."""
        return x
    def xmlrpc_getoutput(self):
        shell_command = "echo before sleep; sleep 10; echo after sleep"
        start = timer()
        d = utils.getProcessOutput("/bin/sh", ["-c", shell_command])
        print("Deferred created in %.2f seconds" % (timer() - start,)) # instant
        return d

reactor.listenTCP(9657, server.Site(Example()))
reactor.run()

And the corresponding xmlrpc client: 以及相应的xmlrpc客户端:

#!/usr/bin/env python
from timeit import default_timer as timer
import xmlrpclib

s = xmlrpclib.Server('http://localhost:9657/')

def report_delay(func, *args):
    start = timer()
    print("Result %r took us %.2f seconds" % (func(*args), timer() - start))

report_delay(s.echo, "Mor-ee-air-teeeee") # should be instant
report_delay(s.getoutput) # should be ~10 seconds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM