V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ri0day
V2EX  ›  Python

python BaseHTTPRequestHandler + cgi.FieldStorage 处理简单post 请求离奇的慢

  •  
  •   ri0day ·
    ri0day · 2011-12-21 13:40:27 +08:00 · 8554 次点击
    这是一个创建于 4503 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家好,我是python 新手,主要为了练习一下python 网络编程。所以自己试着封装了一个memcache的http接口,就用了2个模块 httpserver 和cgi 代码如下:

    https://gist.github.com/1500628

    这个基本是依葫芦画瓢来的。但是我发现 我每次请求一次这个接口,无论是add,delete 还是get 都差不多需要2秒的时间。每次的时间固定在2.007s 2.006s 左右

    [root@esf ~]# time curl -F "authkey=aaa" -F "action=get" -F "cachekey=wumin" http://10.10.93.16:801
    bs2
    real 0m2.007s
    user 0m0.001s
    sys 0m0.003s

    我想知道为什么会出现这种情况。如果我直接用pylibmc操作memcache 是很快的。包了一层http接口会慢一点点。但是也不至于这么慢啊。网上说cgi.FieldStorage处理上传文件很慢。但我这个根本不上传文件。只是从post的数据中拿几个参数出来 对memcache做操作而已.

    我网上搜索了一些profile 工具(hotshot),我对程序也进行了一次profile。但是发现耗时的地方竟然是 基础库socketserver里的readline。到这里我就不知道如何做了。请大家指点一下。

    ncalls tottime percall cumtime percall filename:lineno(function)
    21 1.997 0.095 1.997 0.095 /usr/local/lib/python2.6/socket.py:373(readline)

    以下是我的profile的完整输出:
    http://dpaste.com/675668/

    我尝试过网上说的一些可能解决办法。比如重写BaseHTTPServer 里反向解析客户端IP的那段代码:
    host, port = self.client_address[:2]
    # return socket.getfqdn(host)
    return host

    还是没有效果。请指教一下。这问题到底出在哪里。
    1 条回复    1970-01-01 08:00:00 +08:00
    ri0day
        1
    ri0day  
    OP
       2011-12-21 15:00:12 +08:00
    已经解决了。不是代码问题。是curl POST的问题。我用代码写post请求返回很快。至于具体是curl什么问题 我去研究一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5666 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 142ms · UTC 06:15 · PVG 14:15 · LAX 23:15 · JFK 02:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.