将gevent与python xmlrpclib一起使用 [英] Using gevent with python xmlrpclib

查看:92
本文介绍了将gevent与python xmlrpclib一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在gevent中使用python的标准库xmlrpclib?目前,我尝试使用monkey.patch_all(),但没有成功.

Is it possible to use python's standard libs xmlrpclib with gevent? Currently i'm tried 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正在阻止服务器.在我看来,monkey.patch_all没有修补套接字,这就是它阻止的原因.

urllib2.urlopen is blocking server. It looks to me, that monkey.patch_all did not patched socket, that's why it blocks.

推荐答案

套接字已打补丁,但您的代码还有其他问题.

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

首先,这个

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

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

您在此处生成了一个新的greenlet,但随后将其阻塞直到完成新的greenlet.它不会使事物并发.与运行urlopen并等待其完成完全相同.

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.

第二,为了利用 gevent 的优势,必须同时运行多个轻量级线程(greenlet).

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

SimpleXMLRPCServer被定义为

SimpleXMLRPCServer, however, is defined as

class SimpleXMLRPCServer(SocketServer.TCPServer,
                         SimpleXMLRPCDispatcher):

这意味着它一次服务一个连接.

which means it serves one connection at a time.

如果您创建自己的SimpleXMLRPCServer类,但使用ThreadingTCPServer而不是TCPServer,则应该能够从此处使用gevent受益.

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() 修补threading使其成为基于Greenlet的服务器,因此此类服务器将为每个服务器生成一个新的greenlet新的连接.

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

这篇关于将gevent与python xmlrpclib一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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