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:
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.
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()
Here's an example xmlrpc server that returns a deferred subprocess output:
#!/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:
#!/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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.