Browse Source

[Python] Add Flask with socketify.py WSGI (#7763)

* Added a benchmark test for the socketify.py Python/PyPy web framework

* removed alpine docker

* updated readme.md

* No need to manually add Date header anymore on Socketify.py

* updated socketify.py benchmark to use object factories to increase performance

* add object factory to app-python3.py in socketify

* [Python] Update socketify.py with ASGI and SSGI, add Falcon ASGI SSGI with socketify.py

* fix merge erros

* remove \n from plaintext in socketify.py-wsgi plaintext

* [Python] add Flask with socketify.py ASGI and WSGI

* fix

* fix benchmark_config.json for flask with socketify.py-wsgi
Ciro Spaciari 2 years ago
parent
commit
f2aa640d56

+ 47 - 0
frameworks/Python/flask/app-socketify-wsgi.py

@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+from flask import Flask, make_response, jsonify
+import os
+import multiprocessing
+import logging
+from socketify import WSGI
+# setup
+app = Flask(__name__)
+app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
+
+
[email protected]("/json")
+def hello():
+    return jsonify(message="Hello, World!")
+
[email protected]("/plaintext")
+def plaintext():
+    """Test 6: Plaintext"""
+    response = make_response(b"Hello, World!")
+    response.content_type = "text/plain"
+    return response
+
+
+
+_is_travis = os.environ.get('TRAVIS') == 'true'
+
+workers = int(multiprocessing.cpu_count())
+if _is_travis:
+    workers = 2
+
+
+def run_app():
+    WSGI(app).listen(8080, lambda config: logging.info(f"Listening on http://localhost:{config.port} now\n")).run()
+
+
+def create_fork():
+    n = os.fork()
+    # n greater than 0 means parent process
+    if not n > 0:
+        run_app()
+
+
+# fork limiting the cpu count - 1
+for i in range(1, multiprocessing.cpu_count()):
+    create_fork()
+
+run_app()  # run app on the main process too :)

+ 38 - 0
frameworks/Python/flask/benchmark_config.json

@@ -47,6 +47,44 @@
       "notes": "",
       "versus": "wsgi"
     },
+    "socketify-wsgi": {
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "Postgres",
+      "framework": "flask",
+      "language": "Python",
+      "flavor": "Python3",
+      "orm": "Full",
+      "platform": "None",
+      "webserver": "Socketify.py",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Flask [socketify.py WSGI]",
+      "notes": "",
+      "versus": "wsgi"
+    },
+    "socketify-wsgi-pypy": {
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "Postgres",
+      "framework": "flask",
+      "language": "Python",
+      "flavor": "PyPy",
+      "orm": "Full",
+      "platform": "None",
+      "webserver": "Socketify.py",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Flask [socketify.py WSGI PyPy3]",
+      "notes": "PyPy",
+      "versus": "wsgi"
+    },
     "pypy": {
       "json_url": "/json",
       "db_url": "/db",

+ 26 - 0
frameworks/Python/flask/config.toml

@@ -69,6 +69,32 @@ platform = "Meinheld"
 webserver = "None"
 versus = "wsgi"
 
+[socketify-wsgi]
+urls.plaintext = "/plaintext"
+urls.json = "/json"
+approach = "Realistic"
+classification = "Micro"
+database = "Postgres"
+database_os = "Linux"
+os = "Linux"
+orm = "Full"
+platform = "None"
+webserver = "Socketify.py"
+versus = "wsgi"
+
+[socketify-wsgi-pypy]
+urls.plaintext = "/plaintext"
+urls.json = "/json"
+approach = "Realistic"
+classification = "Micro"
+database = "Postgres"
+database_os = "Linux"
+os = "Linux"
+orm = "Full"
+platform = "None"
+webserver = "Socketify.py"
+versus = "wsgi"
+
 [pypy]
 urls.plaintext = "/plaintext"
 urls.json = "/json"

+ 12 - 0
frameworks/Python/flask/flask-socketify-wsgi-pypy.dockerfile

@@ -0,0 +1,12 @@
+FROM pypy:3.9-bullseye
+
+RUN apt-get update
+RUN apt-get install libpq-dev python3-dev libuv1 -y
+ADD ./requirements-socketify.txt /flask/requirements.txt
+RUN pip3 install -r /flask/requirements.txt
+ADD ./ /flask
+WORKDIR /flask
+
+EXPOSE 8080
+
+CMD python ./app-socketify-wsgi.py

+ 13 - 0
frameworks/Python/flask/flask-socketify-wsgi.dockerfile

@@ -0,0 +1,13 @@
+FROM python:3.10-bullseye
+
+
+RUN apt-get update
+RUN apt-get install libpq-dev python3-dev libuv1 -y
+ADD ./requirements-socketify.txt /flask/requirements.txt
+RUN pip3 install -r /flask/requirements.txt
+ADD ./ /flask
+WORKDIR /flask
+
+EXPOSE 8080
+
+CMD python ./app-socketify-wsgi.py

+ 3 - 0
frameworks/Python/flask/requirements-socketify.txt

@@ -0,0 +1,3 @@
+Flask==2.2.2
+git+https://github.com/cirospaciari/socketify.py.git@main#socketify
+