Python.龙卷风.非阻塞 xmlrpc 客户端 [英] Python. Tornado. Non-blocking xmlrpc client

查看:38
本文介绍了Python.龙卷风.非阻塞 xmlrpc 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我们可以通过以下方式调用 xmlrpc 处理程序:

Basically we can call xmlrpc handlers following way:

import xmlrpclib
s = xmlrpclib.ServerProxy('http://remote_host/rpc/')
print s.system.listmethods()

在 Tornado 中,我们可以像这样集成它:

In tornado we can integrate it like this:

import xmlrpclib
import tornado.web

s = xmlrpclib.ServerProxy('http://remote_host/rpc/')

class MyHandler(tornado.web.RequestHandler):
    def get(self):
        result = s.system.listmethods()

我有以下问题,有点新手,问题:

I have following, a little bit newbie, questions:

  1. result = s.system.listmethods() 会阻止龙卷风吗?
  2. 是否有任何非阻塞的 xmlrpc 客户端?
  3. 如何实现result = yield gen.Task(s.system.listmethods)?
  1. Will result = s.system.listmethods() block tornado?
  2. Are there any non-blocking xmlrpc clients around?
  3. How can we achieve result = yield gen.Task(s.system.listmethods)?

推荐答案

1.是的,它会阻塞 tornado,因为 xmlrpclib 使用阻塞 python 套接字(原样)

1.Yes it will block tornado, since xmlrpclib uses blocking python sockets (as it is)

2.我不知道,但我会提供一个解决方案,您可以保留 xmlrpclib 但使其异步

2.Not that I'm aware of, but I'll provide a solution where you can keep xmlrpclib but have it async

3.我的解决方案不使用龙卷风发电机.

3.My solution doesn't use tornado gen.

好的,所以每当您进行网络连接并需要编写异步代码时,一个有用的库就是 gevent,这是一个非常好的高质量库,我会向所有人推荐.

Ok, so one useful library to have at mind whenever you're doing networking and need to write async code is gevent, it's a really good high quality library that I would recommend to everyone.

为什么好用又好用?

  • 您可以以同步方式编写异步代码(这样就很容易了)
  • 你所要做的就是用一条简单的线来做猴子补丁:

  • You can write asynchronous code in a synchronous manner (so that makes it easy)
  • All you have to do, to do so is monkey patch with one simple line :

来自 gevent 导入猴子;猴子.patch_all()

from gevent import monkey; monkey.patch_all()

使用龙卷风时,您需要知道两件事(您可能已经知道):

When using tornado you need to know two things (that you may already know) :

  • Tornado 在充当 HTTPServer 时仅支持异步视图(异步视图不支持 WSGI)
  • 异步视图需要通过使用 self.finish() 或 self.render()(调用 self.finish())自行终止响应

好的,这里有一个示例,说明了必要的 gevent 与 tornado 集成所需的内容:

Ok so here's an example illustrating what you would need with the necessary gevent integration with tornado :

# Python immports
import functools

# Tornado imports
import tornado.ioloop
import tornado.web
import tornado.httpserver

# XMLRpc imports
import xmlrpclib


# Asynchronous gevent decorator
def gasync(func):
    @tornado.web.asynchronous
    @functools.wraps(func)
    def f(self, *args, **kwargs):
        return gevent.spawn(func, self, *args, **kwargs)
    return f


# Our XML RPC service
xml_service = xmlrpclib.ServerProxy('http://remote_host/rpc/')


class MyHandler(tornado.web.RequestHandler):
    @gasync
    def get(self):
        # This doesn't block tornado thanks to gevent
        # Which patches all of xmlrpclib's socket calls
        # So they no longer are blocking
        result = xml_service.system.listmethods()

        # Do something here

        # Write response to client
        self.write('hello')
        self.finish()


# Our URL Mappings
handlers = [
   (r"/", MyHandler),
]


def main():
    # Setup app and HTTP server
    application = tornado.web.Application(handlers)
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8000)

    # Start ioloop
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    main()

所以试试这个例子(显然要根据你的需要调整它),你应该很高兴.

So give the example a try (adapt it to your needs obviously) and you should be good to go.

无需编写任何额外的代码,gevent 完成了修补 Python 套接字的所有工作,因此它们可以异步使用,同时仍然以同步方式编写代码(这是一个真正的好处).

No need to write any extra code, gevent does all the work of patching up python sockets so they can be used asynchronously while still writing code in a synchronous fashion (which is a real bonus).

希望这有帮助:)

这篇关于Python.龙卷风.非阻塞 xmlrpc 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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