Using gevent with python xmlrpclib

Is it possible to use python standard libs xmlrpclib with gevent? I am currently trying to use monkey.patch_all (), but without success.

from gevent import monkey monkey.patch_all() import gevent import time import xmlrpclib from SimpleXMLRPCServer import SimpleXMLRPCServer import urllib2 def fetch(url): g = gevent.spawn(urllib2.urlopen, url) return g.get().read() def is_even(n): return n%2 == 0 def req(url): return fetch(url) server = SimpleXMLRPCServer(("localhost", 8000)) print "Listening on port 8000..." server.register_function(is_even, "is_even") server.register_function(req, "req") server.serve_forever() 

urllib2.urlopen is blocking the server. It seems to me that monkey.patch_all did not fix the socket, so it blocks.

+4
source share
1 answer

The socket is fixed normally, but there are other problems with your code.

The first is

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

coincides with

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

Here you create a new green, but then block the current one until this new one is executed. This does not make things parallel. This is just like starting urlopen and waiting for it to complete.

Secondly, in order to take advantage of gevent , at the same time there must be more than one thread (greenlet).

SimpleXMLRPCServer, however, is defined as

 class SimpleXMLRPCServer(SocketServer.TCPServer, SimpleXMLRPCDispatcher): 

which means that it serves one connection at a time.

If you create your own SimpleXMLRPCServer class, but use ThreadingTCPServer instead of TCPServer , you can use gevent here.

monkey.patch_all() patches threading to become fundamental, so that such a server will generate new greens for each new connection.

+9
source

All Articles