Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why gevent response time is much less than the real response time while high concurrency #1978

Open
XGiton opened this issue Jul 27, 2023 · 0 comments

Comments

@XGiton
Copy link

XGiton commented Jul 27, 2023

  • gevent version: gevent==20.9.0(pypi).
  • Python version: Python 3.6.9
  • Operating System: Ubuntu 18.04

Description:

I built a flask application and use gevent monkey_patch() to support high concurrency, and use nginx upstream for reverse proxy. everything is ok but occurred lots of 499 http status code responses yesterday.

The 499 status code is returned by nginx because of connection closed by client with setting timeout=5, but all request's response time lte 1s in flask app request log, and CPU usage not full, only reach 60%, same as usual.

So I wrote a demo to test that scene as below:

import click
import json
from gevent import monkey
monkey.patch_all()

from time import sleep
from flask import Flask, jsonify, request
from gevent.pywsgi import WSGIServer


def create_app():
    flask_app = Flask(__name__)

    @flask_app.route('/test/<q_id>', methods=['GET'])
    def test(q_id):
        # for timeout easily
        sleep(0.1)
        # for high cpu usage
        for i in range(1000):
            json.dumps(request.cookies.to_dict())
        return jsonify(result='ok'), 200

    return flask_app


@click.command()
@click.option('--port', help='port')
def main(port):
    port = int(port)
    app = create_app()
    http_server = WSGIServer(('0.0.0.0', port), app)
    http_server.serve_forever()


if __name__ == '__main__':
    main()

I run this app and send lost of concurrency requests with timeout=0.3 on another server. several minutes later, much 499 status code appeared in nginx log like below screenshot:
image

And this is flask app log, request with id=x-333:
image

And CPU usage is 60%, so why the response time in nginx is gte 0.3s, but only 0.146s in gevent + flask app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant