Browse Source

[Python] Add Django socketify.py WSGI (#7782)

* 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 Django socketify.py WSGi

* fix json in django with socketify
Ciro Spaciari 2 years ago
parent
commit
6be1879f50

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

@@ -24,6 +24,44 @@
       "notes": "CPython 3.6",
       "notes": "CPython 3.6",
       "versus": "wsgi"
       "versus": "wsgi"
     },
     },
+    "socketify-wsgi": {
+      "plaintext_url" : "/plaintext",
+      "json_url": "/json",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "django",
+      "language": "Python",
+      "flavor": "Python3",
+      "orm": "Full",
+      "platform": "None",
+      "webserver": "Socketify.py",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Django [Socketify.py WSGI]",
+      "notes": "CPython 3.6",
+      "versus": "wsgi"
+    },
+    "socketify-wsgi-pypy": {
+      "plaintext_url" : "/plaintext",
+      "json_url": "/json",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "django",
+      "language": "Python",
+      "flavor": "PyPy3",
+      "orm": "Full",
+      "platform": "None",
+      "webserver": "Socketify.py",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Django [Socketify.py WSGI PyPy3]",
+      "notes": "",
+      "versus": "wsgi"
+    },
     "postgresql": {
     "postgresql": {
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/dbs?queries=",
       "query_url": "/dbs?queries=",

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

@@ -18,6 +18,32 @@ platform = "None"
 webserver = "Meinheld"
 webserver = "Meinheld"
 versus = "wsgi"
 versus = "wsgi"
 
 
+[socketify-wsgi]
+urls.plaintext = "/plaintext"
+urls.json = "/json"
+approach = "Realistic"
+classification = "Fullstack"
+database = "MySQL"
+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 = "Fullstack"
+database = "MySQL"
+database_os = "Linux"
+os = "Linux"
+orm = "Full"
+platform = "None"
+webserver = "Socketify.py"
+versus = "wsgi"
+
 [postgresql]
 [postgresql]
 urls.db = "/db"
 urls.db = "/db"
 urls.query = "/dbs?queries="
 urls.query = "/dbs?queries="

+ 11 - 0
frameworks/Python/django/django-socketify-wsgi-pypy.dockerfile

@@ -0,0 +1,11 @@
+FROM pypy:3.9-bullseye
+
+ADD ./ /django
+
+WORKDIR /django
+RUN apt-get update; apt-get install libuv1 -y
+RUN pip install -r /django/requirements-socketify.txt
+
+EXPOSE 8080
+
+CMD python ./django-socketify-wsgi.py

+ 11 - 0
frameworks/Python/django/django-socketify-wsgi.dockerfile

@@ -0,0 +1,11 @@
+FROM python:3.10-bullseye
+
+ADD ./ /django
+
+WORKDIR /django
+RUN apt-get update; apt-get install libuv1 -y
+RUN pip install -r /django/requirements-socketify.txt
+
+EXPOSE 8080
+
+CMD python ./django-socketify-wsgi.py

+ 31 - 0
frameworks/Python/django/django-socketify-wsgi.py

@@ -0,0 +1,31 @@
+from socketify import WSGI
+import os
+import multiprocessing
+import logging
+
+from hello_socketify.hello.wsgi import application
+
+
+_is_travis = os.environ.get('TRAVIS') == 'true'
+
+workers = int(multiprocessing.cpu_count())
+if _is_travis:
+    workers = 2
+
+
+def run_app():
+    WSGI(application).listen(8080, lambda config: logging.info(f"Listening on port 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 :)

+ 0 - 0
frameworks/Python/django/hello_socketify/hello/__init__.py


+ 52 - 0
frameworks/Python/django/hello_socketify/hello/settings.py

@@ -0,0 +1,52 @@
+import os
+
+DEBUG = False
+
+SECRET_KEY = '_7mb6#v4yf@qhc(r(zbyh&z_iby-na*7wz&-v6pohsul-d#y5f'
+ADMINS = ()
+
+MANAGERS = ADMINS
+
+DATABASES = {}
+
+TIME_ZONE = 'America/Chicago'
+LANGUAGE_CODE = 'en-us'
+USE_I18N = False
+USE_L10N = False
+USE_TZ = False
+
+MEDIA_ROOT = ''
+MEDIA_URL = ''
+STATIC_ROOT = ''
+STATIC_URL = '/static/'
+STATICFILES_DIRS = ()
+STATICFILES_FINDERS = ()
+MIDDLEWARE = ()
+
+ROOT_URLCONF = 'hello_socketify.hello.urls'
+WSGI_APPLICATION = 'hello_socketify.hello.wsgi.application'
+
+TEMPLATES = [
+    {
+        'BACKEND': 'django.template.backends.django.DjangoTemplates',
+        'DIRS': [],
+        'APP_DIRS': True,
+        'OPTIONS': {},
+    },
+]
+
+INSTALLED_APPS = (
+    'django.contrib.contenttypes',
+    'django.contrib.sessions',
+    'hello_socketify.world',
+)
+
+LOGGING = {
+    'version': 1,
+    'disable_existing_loggers': True,
+    'handlers': {},
+    'loggers': {},
+
+}
+
+ALLOWED_HOSTS = ['*']

+ 7 - 0
frameworks/Python/django/hello_socketify/hello/urls.py

@@ -0,0 +1,7 @@
+from django.urls import re_path
+from ..world.views import plaintext, json
+
+urlpatterns = [
+    re_path(r'^plaintext$', plaintext),
+    re_path(r'^json$', json)
+]

+ 21 - 0
frameworks/Python/django/hello_socketify/hello/wsgi.py

@@ -0,0 +1,21 @@
+"""
+WSGI config for hello project.
+
+This module contains the WSGI application used by Django's development server
+and any production WSGI deployments. It should expose a module-level variable
+named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
+this application via the ``WSGI_APPLICATION`` setting.
+
+Usually you will have the standard Django WSGI application here, but it also
+might make sense to replace the whole Django WSGI application with a custom one
+that later delegates to the Django one. For example, you could introduce WSGI
+middleware here, or combine a Django application with an application of another
+framework.
+
+"""
+import os
+
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_socketify.hello.settings")
+
+from django.core.wsgi import get_wsgi_application
+application = get_wsgi_application()

+ 0 - 0
frameworks/Python/django/hello_socketify/world/__init__.py


+ 14 - 0
frameworks/Python/django/hello_socketify/world/views.py

@@ -0,0 +1,14 @@
+from json import dumps
+
+from django.http import HttpResponse
+
+
+def plaintext(request):
+    return HttpResponse("Hello, World!", content_type="text/plain")
+
+
+def json(request):
+    return HttpResponse(
+            dumps({"message": "Hello, World!"}),
+            content_type="application/json"
+        )

+ 2 - 0
frameworks/Python/django/requirements-socketify.txt

@@ -0,0 +1,2 @@
+Django==4.1.3
+git+https://github.com/cirospaciari/socketify.py.git@main#socketify