Using gevent with python xmlrpclib

只谈情不闲聊 提交于 2019-12-04 10:00:42

The socket is patched fine, but there are other problems with your code.

First, this

def fetch(url):
    g = gevent.spawn(urllib2.urlopen, url)
    return g.get().read()

is the same as

def fetch(url):
    return urllib2.urlopen(url).read()

You're spawning a new greenlet here but then blocking the current one until that new one is done. It does not make things concurrent. It's exactly the same as just running urlopen and waiting for it to finish.

Second, in order to take advantage of gevent there got to be more than one lightweight thread (greenlet) running at the same time.

SimpleXMLRPCServer, however, is defined as

class SimpleXMLRPCServer(SocketServer.TCPServer,
                         SimpleXMLRPCDispatcher):

which means it serves one connection at a time.

If you make your own SimpleXMLRPCServer class, but use ThreadingTCPServer instead of TCPServer, you should be able to benefit from using gevent here.

monkey.patch_all() patches threading to become greenlet-based, so such server will spawn a new greenlet for each new connection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!