From f2f64a81321a8f0c2f1f13311f93c185ee17af8c Mon Sep 17 00:00:00 2001 From: James Roberts Date: Thu, 21 Oct 2021 23:21:16 +0200 Subject: [PATCH] Add usage example to README --- README.md | 32 +++++++++++++++++++++++++------- fast-wsgi/request.c | 4 ++-- fast_wsgi.py | 14 +++----------- load-test.sh | 2 +- run.sh | 2 +- testapp.py | 15 +++++++++++++++ 6 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 testapp.py diff --git a/README.md b/README.md index 13aeba6..fd53dd6 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,36 @@ [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/jamesroberts/fast-wsgi.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/jamesroberts/fast-wsgi/context:python) # Fast WSGI +#### Note: Fast WSGI is still under development... Fast WSGI is an ultra fast WSGI server for Python 3. It is mostly written in C. It makes use of [libuv](https://github.com/libuv/libuv) and [llhttp](https://github.com/nodejs/llhttp) under the hood for blazing fast performance. -** Fast WSGI is still under development... + + +## Example usage with Flask + +```python +import fast_wsgi +from flask import Flask + +app = Flask(__name__) + + +@app.get("/") +def hello_world(): + return "Hello, World!", 200 + + +if __name__ == "__main__": + fast_wsgi.run(wsgi_app=app, host="0.0.0.0", port=5000) +``` ## TODO -- Memory lifecycle management -- Python object ref count tracking -- WSGI app invoking -- Test integration with flask app -- Basic error handling - +- Test integration with other frameworks (uWSGI, Django, etc) +- Comprehensive error handling +- Complete HTTP/1.1 compliance +- Test on multiple platforms (Windows/MacOS) +- Unit Tests \ No newline at end of file diff --git a/fast-wsgi/request.c b/fast-wsgi/request.c index 28c984c..074013e 100644 --- a/fast-wsgi/request.c +++ b/fast-wsgi/request.c @@ -225,8 +225,8 @@ void build_response(PyObject* wsgi_response, StartResponse* response, llhttp_t* PyObject* close_result = PyObject_CallObject(close, NULL); Py_XDECREF(close_result); } - Py_DECREF(close); - Py_DECREF(iter); + Py_XDECREF(iter); + Py_XDECREF(close); Py_XDECREF(result); result = NULL; } diff --git a/fast_wsgi.py b/fast_wsgi.py index e18a5e1..55072a6 100644 --- a/fast_wsgi.py +++ b/fast_wsgi.py @@ -2,7 +2,6 @@ import os import signal import _fast_wsgi -from flask import Flask, request NUM_WORKERS = 4 HOST = "0.0.0.0" @@ -33,15 +32,8 @@ def run_multi_process_server(app): os.kill(worker, signal.SIGINT) -app = Flask(__name__) - - -@app.route("/test", methods=["GET"]) -def hello_world(): - return {"message": "Hello, World!"}, 200 - - -if __name__ == "__main__": +def run(wsgi_app, host, port, backlog=1024): print("Starting server...") + enable_logging = 0 + _fast_wsgi.run_server(wsgi_app, host, port, backlog, enable_logging) # run_multi_process_server(app) - _fast_wsgi.run_server(app, HOST, PORT, BACKLOG, 1) diff --git a/load-test.sh b/load-test.sh index ba761d3..af99aa9 100644 --- a/load-test.sh +++ b/load-test.sh @@ -1 +1 @@ -wrk -t8 -c100 -d15 http://localhost:5000/test \ No newline at end of file +wrk -t8 -c100 -d15 http://localhost:5000 \ No newline at end of file diff --git a/run.sh b/run.sh index c6baa42..8885121 100644 --- a/run.sh +++ b/run.sh @@ -1 +1 @@ -sudo python3 setup.py install; python3 fast_wsgi.py; fuser -k 5000/tcp; +sudo python3 setup.py install; python3 testapp.py; fuser -k 5000/tcp; diff --git a/testapp.py b/testapp.py new file mode 100644 index 0000000..7d5b6c3 --- /dev/null +++ b/testapp.py @@ -0,0 +1,15 @@ +import fast_wsgi +import bjoern +from flask import Flask + +app = Flask(__name__) + + +@app.get("/") +def hello_world(): + return "Hello, World!", 200 + + +if __name__ == "__main__": + # fast_wsgi.run(wsgi_app=app, host="0.0.0.0", port=5000) + bjoern.run(app, "0.0.0.0", 5000)