gpt4 book ai didi

python - 在Tornado中进行异步xmlrpc(客户端调用)

转载 作者:行者123 更新时间:2023-12-01 05:35:07 24 4
gpt4 key购买 nike

我需要对 Tornado 中的 Cobbler 服务器进行异步 XML-RPC(客户端调用),例如如下所示:

cobbler_connection = xmlrpclib.Server("http://my_cobbler_server")

...
profile = yield gen.Task(cobbler_connection.get_profile, profile_name)
...

现在我正在使用 xmlrpclib,它似乎不会进行异步客户端调用,因此它最终会阻止 Tornado(或者我是这么听说的)。当涉及到 Tornado 和异步编程时,我是一个新手(但请不要给我任何说明或初学者指南的链接)。我尝试过谷歌搜索,但我无法清楚地了解需要对 xmlrpclib 进行哪些更改,以便我可以使这些调用异步。任何指示将不胜感激。提前致谢。

最佳答案

检查 Twisted 是否是一个好主意支持各种操作,因为它可以使用tornado.platform.twisted与Tornado结合。对于 XML-RPC 有以下代码:http://twistedmatrix.com/documents/13.0.0/web/howto/xmlrpc.html一个简单的例子:

import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.platform.twisted

tornado.platform.twisted.install()

from twisted.web.xmlrpc import Proxy
from twisted.internet import reactor

proxy = Proxy('http://advogato.org/XMLRPC')

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
def printValue(self, value):
self.write(repr(value))
self.finish()

def printError(self, error):
self.write('error: %s' % error)
self.finish()

@tornado.web.asynchronous
def get(self):
proxy.callRemote('test.sumprod', 3, 5).addCallbacks(self.printValue, self.printError)

if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

基准测试:

$ ab -n 1000 -c 5 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software: TornadoServer/3.0.1
Server Hostname: localhost
Server Port: 8000

Document Path: /
Document Length: 7 bytes

Concurrency Level: 5
Time taken for tests: 76.529 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 201000 bytes
HTML transferred: 7000 bytes
Requests per second: 13.07 [#/sec] (mean)
Time per request: 382.646 [ms] (mean)
Time per request: 76.529 [ms] (mean, across all concurrent requests)
Transfer rate: 2.56 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 302 382 498.9 308 5318
Waiting: 302 382 498.9 308 5318
Total: 302 382 498.9 308 5318

Percentage of the requests served within a certain time (ms)
50% 308
66% 310
75% 311
80% 313
90% 320
95% 334
98% 1310
99% 3309
100% 5318 (longest request)

直接使用xmlrpclib进行比较:

import tornado.httpserver
import tornado.ioloop
import tornado.web

import xmlrpclib

server = xmlrpclib.ServerProxy('http://advogato.org/XMLRPC')

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write(repr(server.test.sumprod(3, 5)))

if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

基准测试:

$  ab -n 1000 -c 5 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software: TornadoServer/3.0.1
Server Hostname: localhost
Server Port: 8000

Document Path: /
Document Length: 7 bytes

Concurrency Level: 5
Time taken for tests: 325.538 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 201000 bytes
HTML transferred: 7000 bytes
Requests per second: 3.07 [#/sec] (mean)
Time per request: 1627.690 [ms] (mean)
Time per request: 325.538 [ms] (mean, across all concurrent requests)
Transfer rate: 0.60 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 305 1625 484.5 1532 4537
Waiting: 305 1624 484.5 1532 4537
Total: 305 1625 484.5 1532 4537

Percentage of the requests served within a certain time (ms)
50% 1532
66% 1535
75% 1537
80% 1539
90% 1547
95% 1984
98% 4524
99% 4533
100% 4537 (longest request)

更糟糕。

关于python - 在Tornado中进行异步xmlrpc(客户端调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com