Skip to content

Commit

Permalink
adding gunicorn.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
ali96343 committed Nov 5, 2023
1 parent 69033c2 commit 3279540
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 56 deletions.
112 changes: 112 additions & 0 deletions py4web/gunicorn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
gunicorn
~~~~~~~~

The gunicorn server starts in the usual way for the py4web

::

$./py4web.py run apps -s gunicorn --watch=off
$
$./py4web.py run apps -s gunicornGevent --watch=off
$
$ # gunicornGevent === gunicorn + monkey.patch_all()

It is possible to use several methods to configure gunicorn options with py4web

Let's show examples

* set gunicorn options via bash environment variables

::

$export GUNICORN_worker_class=sync
$ ./py4web.py run apps -s gunicorn -L 20 -w 4 --watch=off
$
$export GUNICORN_worker_class=gthread
$ ./py4web.py run apps -s gunicorn -L 20 -w 4 --watch=off
$
$export GUNICORN_worker_class=gevent
$ ./py4web.py run apps -s gunicornGevent -L 20 -w 4 --watch=off
$
$export GUNICORN_worker_class=eventlet
$ ./py4web.py run apps -s gunicornGevent -L 20 -w 4 --watch=off



* set gunicorn options via config file py4web/gunicorn.saenv

::

export GUNICORN_worker_tmp_dir=/dev/shm
export GUNICORN_max_requests=1200
worker_class=gthread
threads=2

# guncornGevent
#worker_class=gevent
#worker_class=eventlet

# for use python-config-file
# use_python_config=myguni.conf.py

# for use python-config-mod_name
# use_python_config=python:mod_name


* set gunicorn options via python file py4web/myguni.conf.py

::

set the variable use_python_config=myguni.conf.py

.. code:: bash
$ # via env
$export GUNCORN_use_python_config=myguni.conf.py
$
$ # via py4web/gunicorn.saenv
$echo use_python_config=mmyguni.conf.py >> py4web/gunicorn.saenv
::

write file py4web/myguni.conf.py

.. code:: python
# Gunicorn configuration file
# https://docs.gunicorn.org/en/stable/settings.html
import multiprocessing
max_requests = 1000
max_requests_jitter = 50
log_file = "-"
workers = multiprocessing.cpu_count() * 2 + 1
::

./py4web.py run apps -s gunicorn

* set gunicorn options via python module

::

write python module

.. code:: bash
$ cd py4web && mkdir mod_name && cp myguni.conf.py mod_name/__init__.py
$
$ # via env
$export GUNCORN_use_python_config=python:mod_name
$
$ # via py4web/gunicorn.saenv
$echo use_python_config=python:mod_name >> py4web/gunicorn.saenv
::

./py4web.py run apps -s gunicorn

57 changes: 1 addition & 56 deletions py4web/server_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ def gunicorn():
from gevent import local # pip install gevent gunicorn
import threading

# To use gevent monkey.patch_all()
# run ./py4web.py run apps -s gunicornGevent ...

if isinstance(threading.local(), local.local):
print("gunicorn: monkey.patch_all() applied")

Expand All @@ -139,6 +136,7 @@ class GunicornServer(ServerAdapter):

# ./py4web.py run apps -s gunicorn --watch=off --ssl_cert=cert.pem --ssl_key=key.pem -w 6 -L 20
# ./py4web.py run apps -s gunicornGevent --watch=off --ssl_cert=cert.pem --ssl_key=key.pem -w 6 -L 20
# time seq 1 5000 | xargs -I % -P 0 curl http://localhost:8000/todo &>/dev/null

def run(self, app_handler):
from gunicorn.app.base import Application
Expand Down Expand Up @@ -226,59 +224,6 @@ def check_kv(kx, vx):

def load_config(self):

"""
gunicorn.saenv
# load conf from native_gunicorn
use_native_config=python:example
use_native_config=gunicorn.conf.py
# or
use_python_config=python:example
use_python_config=gunicorn.conf.py
# example
# export GUNICORN_max_requests=1200
export GUNICORN_worker_tmp_dir=/dev/shm
# run as -s gunicornGevent
#worker_class=gevent
#worker_class=eventlet
# run as -s gunicorn
#worker_class=sync
[hello any text]
worker_class=gthread
workers=4
threads=8
"""

# export GUNICORN_BACKLOG=4096
# export GUNICORN_worker_connections=100

# export GUNICORN_worker_class=sync
# export GUNICORN_worker_class=gthread
# export GUNICORN_worker_tmp_dir=/dev/shm
# export GUNICORN_threads=8
# export GUNICORN_timeout=10
# export GUNICORN_max_requests=1200

#
# tested with ssep4w https://github.com/ali96343/lvsio
#
# To use gevent monkey.patch_all()
# run ./py4web.py run apps -s gunicornGevent ......
# export GUNICORN_worker_class=gevent
# export GUNICORN_worker_class=gunicorn.workers.ggevent.GeventWorker
#
# pip install gunicorn[eventlet]
# export GUNICORN_worker_class=eventlet
#
# time seq 1 5000 | xargs -I % -P 0 curl http://localhost:8000/todo &>/dev/null
# time seq 1 5000 | xargs -I % -P 0 curl -k https://localhost:8000/todo &>/dev/null

gunicorn_vars = self.get_gunicorn_vars()

# test https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py
Expand Down

0 comments on commit 3279540

Please sign in to comment.