推荐学习书目
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
eightqueen
V2EX  ›  Python

python 的 cgi 程序没法用 ab 来做性能测试

  •  
  •   eightqueen · Apr 11, 2016 · 2794 views
    This topic created in 3721 days ago, the information mentioned may be changed or developed.
    import socket
    import signal
    import multiprocessing 
    
    response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 11\r\n\r\nHello World'
    
    server = socket.socket()
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(('0.0.0.0', 9527))
    server.listen(1024)
    
    def handler(client):
        request = client.recv(1024)
        client.send(response)
        client.close()
        
    #多进程里的子进程执行完后并不会死掉,而是变成僵尸进程,等待主进程挂掉后才会死掉,下面这条语句可以解决这个问题。
    signal.signal(signal.SIGCHLD,signal.SIG_IGN)
    
    while True:
        client, addr = server.accept()
        process = multiprocessing.Process(target=handler, args=(client,))
        process.start()
    

    ab -n 1 -c 1 http://localhost:9527/

    结果竟然超时,真是无语了

    2 replies    2016-04-12 15:38:00 +08:00
    keakon
        1
    keakon  
       Apr 12, 2016
    最简单的做法是在最后一行加上 client.close(),子进程中只是把它这个 socket 的拷贝给 close 了,但因为父进程中还存在,所以不会关闭。
    julyclyde
        2
    julyclyde  
       Apr 12, 2016
    问题是你这不是 CGI 啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 22:43 · PVG 06:43 · LAX 15:43 · JFK 18:43
    ♥ Do have faith in what you're doing.