ããã¯ããªã«ãããããŠæžãããã®ïŒ
å æ¥ãWSGIãµãŒããŒãšããŠãGunicornãuWSGIãåãããŠã¿ãŸããã
ãã®æã«ããããã®ãµãŒããŒã«ã¯èµ·åæã«ããã»ã¹æ°ãã¹ã¬ããæ°ãäžããããšãã§ãããšããããŸããã
ãšããã§ãè€æ°ããã»ã¹ãè€æ°ã¹ã¬ããã«ããæã«ãã¢ããªã±ãŒã·ã§ã³ããèŠããã©ãããããšãæ°ã«ããæ¹ã
ãããããªãã§ãããããïŒ
WSGIããèŠã
ãŸãã¯ã仿§ã§ããWSGIããèŠãã®ãçã§ãããã
幞ããæ¥æ¬èªèš³ãããããã§ãã®ã§ã
PEP 333: Python Web Server Gateway Interface v1.0 — knzm.readthedocs.org 2012-12-31 documentation
ã¡ãªã¿ã«ãããŸãéåãªä»æ§ã§ããªããããªã®ã§ãå²ãšèªããæããªã®ã§ã¯ãªãããšã
ããã§ããã¹ã¬ãããµããŒããã«ã€ããŠèŠãŠã¿ãŸãã
Thread support, or lack thereof, is also server-dependent. Servers that can run multiple requests in parallel, should also provide the option of running an application in a single-threaded fashion, so that applications or frameworks that are not thread-safe may still be used with that server.
ã¹ã¬ãããµããŒãããããã¯ãã®æ¬ åŠããŸãããµãŒãäŸåã§ãããè€æ°ã®èŠæ± ã䞊åã«å®è¡ããããšãã§ãããµãŒãã¯ãã¹ã¬ããã»ã»ãŒãã§ãªãã¢ããªã±ãŒ ã·ã§ã³ãŸãã¯ãã¬ãŒã ã¯ãŒã¯ãäŸç¶ãšããŠãã®ãµãŒããšå ±ã«äœ¿çšã§ããããã«ã ã·ã³ã°ã«ã¹ã¬ããæ¹åŒã§ã¢ããªã±ãŒã·ã§ã³ãå®è¡ãããªãã·ã§ã³ãæäŸãã¹ã ã§ãã (should)ã
âŠã¯ãã
ãšããããã§ãè€æ°ã¹ã¬ããã§å®è¡ãããããªå Žåã¯ã¢ããªã±ãŒã·ã§ã³åŽãæèãã¹ãã ãšããããã«èŠããŸããã ãã¶
ãµãã£ãšããæãã§ããã
ã¡ãã£ãšããã€ãæ°ã«ãªããã¿ãŒã³ããWSGIãå®è£ ãããµãŒããŒã§ããGunicornãšuWSGIã§è©ŠããŠã¿ãããšã«ããŸãããã
ç°å¢ãšã€ã³ã¹ããŒã«
ä»åã®ç°å¢ã¯ããã¡ãã
$ python3 -V Python 3.6.7
GunicornãšPythonãã€ã³ã¹ããŒã«ã
$ pip3 install gunicorn $ pip3 install uwsgi
ããŒãžã§ã³ã
$ pip3 freeze ... gunicorn==19.9.0 uWSGI==2.0.18 ...
ããã»ã¹æ°ãã¹ã¬ããæ°ã倿ŽããŠã¿ã
ãŸãã¯ãããã»ã¹æ°ãã¹ã¬ããæ°ã調æŽããªããå€åãèŠãŠããããšæããŸãã
ãµã³ãã«ã¢ããªã±ãŒã·ã§ã³ã¯ããã¡ãã
wsgi-app.py
import os import threading def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) pid = os.getpid() thread_name = threading.current_thread().getName() return ['get response / pid = {}, thread-name = {}'.format(pid, thread_name).encode()]
ãã®ã¹ã¯ãªãããå®è¡ããããã»ã¹ã®IDããã¹ã¬ããåãè¿ãããã«ããŠããŸãã
ãã¡ãã䜿ã£ãŠãå€åãèŠãŠãããŸãããã
Gunicorn
æåã¯ãGunicornããšããããããªãã·ã§ã³ãªãã§Gunicornãèµ·åããŸãã
$ gunicorn wsgi-app:application [2019-04-16 23:56:02 +0900] [26240] [INFO] Starting gunicorn 19.9.0 [2019-04-16 23:56:02 +0900] [26240] [INFO] Listening at: http://127.0.0.1:8000 (26240) [2019-04-16 23:56:02 +0900] [26240] [INFO] Using worker: sync [2019-04-16 23:56:02 +0900] [26245] [INFO] Booting worker with pid: 26245
ã·ã³ã°ã«ããã»ã¹ã1ã¹ã¬ããã§ãã
ã¢ã¯ã»ã¹ã
$ curl localhost:8000 get response / pid = 26245, thread-name = MainThread
è€æ°åã¢ã¯ã»ã¹ããŠããã¹ã¬ããã¯åãæ¿ãããŸããã
$ for i in `seq 300`; do curl -s localhost:8000; echo ; done | sort -u get response / pid = 26245, thread-name = MainThread
ããã»ã¹æ°4ãã¹ã¬ããæ°2ã«ããŠã¿ãŸãã
$ gunicorn --workers 4 --threads 2 wsgi-app:application [2019-04-17 00:23:22 +0900] [753] [INFO] Starting gunicorn 19.9.0 [2019-04-17 00:23:22 +0900] [753] [INFO] Listening at: http://127.0.0.1:8000 (753) [2019-04-17 00:23:22 +0900] [753] [INFO] Using worker: threads [2019-04-17 00:23:22 +0900] [756] [INFO] Booting worker with pid: 756 [2019-04-17 00:23:22 +0900] [757] [INFO] Booting worker with pid: 757 [2019-04-17 00:23:22 +0900] [758] [INFO] Booting worker with pid: 758 [2019-04-17 00:23:22 +0900] [760] [INFO] Booting worker with pid: 760
ã¹ã¬ããåããMainThreadãã§ã¯ãªããªããŸããã
$ curl localhost:8000 get response / pid = 760, thread-name = ThreadPoolExecutor-0_0
è€æ°åã¢ã¯ã»ã¹ãããšãããã»ã¹ã4ã€ãã¹ã¬ããã2ã€ã確èªããããšãã§ããŸããããã»ã¹éã§ã¹ã¬ããåãåããªã®ã§
ã¹ã¬ãããåãããŠãããã©ãããããã«ããã§ããâŠã
$ for i in `seq 300`; do curl -s localhost:8000; echo ; done | sort -u get response / pid = 756, thread-name = ThreadPoolExecutor-0_0 get response / pid = 756, thread-name = ThreadPoolExecutor-0_1 get response / pid = 757, thread-name = ThreadPoolExecutor-0_0 get response / pid = 758, thread-name = ThreadPoolExecutor-0_0 get response / pid = 758, thread-name = ThreadPoolExecutor-0_1 get response / pid = 760, thread-name = ThreadPoolExecutor-0_0
psã³ãã³ãã§ç¢ºèªããŠã¿ãŸãããã
$ ps aux -L | grep gunicorn xxxxx 753 753 0.3 1 0.1 75024 23832 pts/2 S+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 756 756 0.1 3 0.1 230944 21444 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 756 845 0.0 3 0.1 230944 21444 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 756 846 0.0 3 0.1 230944 21444 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 757 757 0.2 3 0.1 230944 21452 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 757 793 0.2 3 0.1 230944 21452 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 757 794 0.0 3 0.1 230944 21452 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 758 758 0.2 3 0.1 230944 21456 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 758 799 0.0 3 0.1 230944 21456 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 758 800 0.2 3 0.1 230944 21456 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 760 760 0.2 3 0.1 230944 21472 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 760 777 0.1 3 0.1 230944 21472 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application xxxxx 760 778 0.0 3 0.1 230944 21472 pts/2 Sl+ 00:23 0:00 /path/to/venv/bin/gunicorn --workers 4 --threads 2 wsgi-app:application
ãããã1ããã»ã¹ãããããã£ãšããããã¹ã¬ãããããããã«ãèŠããŸãâŠã©ããªã£ãŠããã§ãããâŠã
ä»åºŠã¯ã4ããã»ã¹ã4ã¹ã¬ããã«ããŠã¿ãŸãã
$ gunicorn --workers 4 --threads 4 wsgi-app:application [2019-04-17 00:26:39 +0900] [1567] [INFO] Starting gunicorn 19.9.0 [2019-04-17 00:26:39 +0900] [1567] [INFO] Listening at: http://127.0.0.1:8000 (1567) [2019-04-17 00:26:39 +0900] [1567] [INFO] Using worker: threads [2019-04-17 00:26:39 +0900] [1570] [INFO] Booting worker with pid: 1570 [2019-04-17 00:26:39 +0900] [1571] [INFO] Booting worker with pid: 1571 [2019-04-17 00:26:39 +0900] [1572] [INFO] Booting worker with pid: 1572 [2019-04-17 00:26:39 +0900] [1573] [INFO] Booting worker with pid: 1573
確èªã
$ curl localhost:8000 get response / pid = 27520, thread-name = ThreadPoolExecutor-0_0
è€æ°åã¢ã¯ã»ã¹ã
$ for i in `seq 300`; do curl -s localhost:8000; echo ; done | sort -u get response / pid = 1570, thread-name = ThreadPoolExecutor-0_0 get response / pid = 1570, thread-name = ThreadPoolExecutor-0_2 get response / pid = 1570, thread-name = ThreadPoolExecutor-0_3 get response / pid = 1571, thread-name = ThreadPoolExecutor-0_0 get response / pid = 1571, thread-name = ThreadPoolExecutor-0_1 get response / pid = 1571, thread-name = ThreadPoolExecutor-0_2 get response / pid = 1572, thread-name = ThreadPoolExecutor-0_0 get response / pid = 1572, thread-name = ThreadPoolExecutor-0_1 get response / pid = 1572, thread-name = ThreadPoolExecutor-0_2 get response / pid = 1572, thread-name = ThreadPoolExecutor-0_3 get response / pid = 1573, thread-name = ThreadPoolExecutor-0_0 get response / pid = 1573, thread-name = ThreadPoolExecutor-0_1 get response / pid = 1573, thread-name = ThreadPoolExecutor-0_2
psã³ãã³ãã§èŠããšããã£ã±ãããããããŸãâŠã
$ ps aux -L | grep gunicorn xxxxx 1567 1567 0.1 1 0.1 75024 23832 pts/2 S+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1570 1570 0.0 5 0.1 378408 21448 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1570 1635 0.0 5 0.1 378408 21448 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1570 1636 0.0 5 0.1 378408 21448 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1570 1687 0.0 5 0.1 378408 21448 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1570 1691 0.0 5 0.1 378408 21448 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1571 1571 0.1 5 0.1 378408 21456 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1571 1602 0.0 5 0.1 378408 21456 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1571 1603 0.0 5 0.1 378408 21456 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1571 1625 0.0 5 0.1 378408 21456 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1571 1626 0.0 5 0.1 378408 21456 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1572 1572 0.1 5 0.1 378408 21460 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1572 1607 0.0 5 0.1 378408 21460 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1572 1608 0.0 5 0.1 378408 21460 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1572 1611 0.0 5 0.1 378408 21460 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1572 1612 0.0 5 0.1 378408 21460 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1573 1573 0.1 5 0.1 378408 21476 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1573 1585 0.0 5 0.1 378408 21476 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1573 1586 0.0 5 0.1 378408 21476 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1573 1598 0.0 5 0.1 378408 21476 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application xxxxx 1573 1599 0.0 5 0.1 378408 21476 pts/2 Sl+ 00:26 0:00 /path/to/venv/bin/python3 /path/to/venv/bin/gunicorn --workers 4 --threads 4 wsgi-app:application
ãããã¡ãããšç¢ºèªããæ¹ãããããã§ããâŠã
https://github.com/benoitc/gunicorn/blob/19.9.0/gunicorn/workers/gthread.py#L101
uWSGI
ç¶ããŠãuWSGIãèŠãŠã¿ãŸãã
ããã©ã«ãã§èµ·åã
$ uwsgi --http :8000 --wsgi-file wsgi-app.py ... spawned uWSGI worker 1 (and the only) (pid: 28159, cores: 1)
1ããã»ã¹ã1ã¯ãŒã«ãŒã§ãã
確èªã
$ curl localhost:8000 get response / pid = 28159, thread-name = uWSGIWorker1Core0
ç¬èªã®ã¹ã¬ããã§ããã
è€æ°åã¢ã¯ã»ã¹ã
$ for i in `seq 300`; do curl -s localhost:8000; echo ; done | sort -u get response / pid = 28159, thread-name = uWSGIWorker1Core0
ç¶ããŠãããã»ã¹æ°4ãã¹ã¬ããæ°2ã«ããŠã¿ãŸãã
$ uwsgi --http :8000 --master --processes 4 --threads 2 --wsgi-file wsgi-app.py ... spawned uWSGI master process (pid: 28788) spawned uWSGI worker 1 (pid: 28789, cores: 2) spawned uWSGI worker 2 (pid: 28790, cores: 2) spawned uWSGI worker 3 (pid: 28791, cores: 2) spawned uWSGI worker 4 (pid: 28792, cores: 2) spawned uWSGI http 1 (pid: 28793)
1åã®ãªã¯ãšã¹ãã§ç¢ºèªã
$ curl localhost:8000 get response / pid = 28792, thread-name = uWSGIWorker4Core0
è€æ°ã¢ã¯ã»ã¹ãããšã1ããã»ã¹ããããã¹ã¬ããã2ã€ãããããªããšã確èªã§ããŸãã
$ for i in `seq 300`; do curl -s localhost:8000; echo ; done | sort -u get response / pid = 28789, thread-name = uWSGIWorker1Core0 get response / pid = 28789, thread-name = uWSGIWorker1Core1 get response / pid = 28790, thread-name = uWSGIWorker2Core0 get response / pid = 28790, thread-name = uWSGIWorker2Core1 get response / pid = 28791, thread-name = uWSGIWorker3Core0 get response / pid = 28791, thread-name = uWSGIWorker3Core1 get response / pid = 28792, thread-name = uWSGIWorker4Core0 get response / pid = 28792, thread-name = uWSGIWorker4Core1
å®éãpsã³ãã³ãã§èŠãŠããã¯ãŒã«ãŒããã»ã¹ãããã2ã¹ã¬ãããåãšããŠäœãããŠããŸããã
ããã»ã¹æ°ãã¹ã¬ããæ°ããšãã«4ã«ããŠã¿ãŸãããã
$ uwsgi --http :8000 --master --processes 4 --threads 4 --wsgi-file wsgi-app.py ... spawned uWSGI master process (pid: 29460) spawned uWSGI worker 1 (pid: 29461, cores: 4) spawned uWSGI worker 2 (pid: 29462, cores: 4) spawned uWSGI worker 3 (pid: 29463, cores: 4) spawned uWSGI worker 4 (pid: 29464, cores: 4) spawned uWSGI http 1 (pid: 29465)
1ãªã¯ãšã¹ãã§ç¢ºèªã
$ curl localhost:8000 get response / pid = 29461, thread-name = uWSGIWorker1Core0
è€æ°åã®ãªã¯ãšã¹ãã§ç¢ºèªã
$ for i in `seq 300`; do curl -s localhost:8000; echo ; done | sort -u get response / pid = 29461, thread-name = uWSGIWorker1Core0 get response / pid = 29461, thread-name = uWSGIWorker1Core1 get response / pid = 29461, thread-name = uWSGIWorker1Core2 get response / pid = 29461, thread-name = uWSGIWorker1Core3 get response / pid = 29462, thread-name = uWSGIWorker2Core0 get response / pid = 29462, thread-name = uWSGIWorker2Core1 get response / pid = 29462, thread-name = uWSGIWorker2Core2 get response / pid = 29462, thread-name = uWSGIWorker2Core3 get response / pid = 29463, thread-name = uWSGIWorker3Core0 get response / pid = 29463, thread-name = uWSGIWorker3Core1 get response / pid = 29463, thread-name = uWSGIWorker3Core2 get response / pid = 29463, thread-name = uWSGIWorker3Core3 get response / pid = 29464, thread-name = uWSGIWorker4Core0 get response / pid = 29464, thread-name = uWSGIWorker4Core1 get response / pid = 29464, thread-name = uWSGIWorker4Core2 get response / pid = 29464, thread-name = uWSGIWorker4Core3
ãã¯ãã4察4ã§ããããã¯ããããããã§ããã
ã¢ããªã±ãŒã·ã§ã³ã®åæååŠçãã©ãèããïŒ
ãšããã§ãWSGIã®ä»æ§ã¯ã¢ããªã±ãŒã·ã§ã³ã®ãšã³ããªãã€ã³ããå®çŸ©ãããŠããã ãã§ãã
Djangoã®ãããªã¢ããªã±ãŒã·ã§ã³ãã¬ãŒã ã¯ãŒã¯ã§ã¯ãèµ·åæã«åæååŠçãšããã£ãã®ã§ãããã©ããªã£ãŠããã®ã§ãããïŒ
Djangoã®WSGIåãã®ãã¡ã€ã«ãèŠãŠã¿ãŸãã
xxproject/xxproject/wsgi.py
""" WSGI config for mysite project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xxproject.settings') application = get_wsgi_application()
ãapplicationãã«é¢æ°ã®æ»ãå€ãæ ŒçŽããŠãããŸãã¿ããã§ãã
ãã®äžèº«ãèŠãŠã¿ãŸãã
https://github.com/django/django/blob/2.2/django/core/wsgi.py
def get_wsgi_application(): """ The public interface to Django's WSGI support. Return a WSGI callable. Avoids making django.core.handlers.WSGIHandler a public API, in case the internal WSGI implementation changes or moves in the future. """ django.setup(set_prefix=False) return WSGIHandler()
ã»ããã¢ãããããŠãå¥ã®ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŠããããã§ãã
ã»ããã¢ããã®äžèº«ã¯ããã¡ãã
https://github.com/django/django/blob/2.2/django/__init__.py
å®éã®WSGIã®ãšã³ããªãã€ã³ããšããŠã¯ããã¡ããæ©èœããŸãããšã
https://github.com/django/django/blob/2.2/django/core/handlers/wsgi.py#L130
ã€ãŸããå§è²å ã®é¢æ°ïŒãšããŠæ©èœãããã®ïŒãè¿ãåã«ãåæååŠçãå ¥ã蟌ãã§ãããšããããã§ããã
ããããããŠããããªãœãŒã¹ã³ãŒããçšæããŸããã
wsgi-app-with-startup.py
import os import threading def startup(): print('application startup!!') return Handler() class Handler(): def __call__(self, environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) pid = os.getpid() thread_name = threading.current_thread().getName() return ['get response / pid = {}, thread-name = {}'.format(pid, thread_name).encode()] application = startup()
å®éã®åŠçã¯ãHandlerãšããã¯ã©ã¹ãè¡ããŸãããHandlerã®ã€ã³ã¹ã¿ã³ã¹ãè¿ãåã«åæååŠçãšããŠã¡ãã»ãŒãžåºåã
è¡ããŸãã
ãã¡ãããèµ·åããããã»ã¹æ°ãå€ãããããªããåäœã確èªããŠã¿ãŸãããã
Gunicorn
ãŸãã¯ãGunicornã
$ gunicorn wsgi-app-with-startup:application [2019-04-17 00:09:44 +0900] [30839] [INFO] Starting gunicorn 19.9.0 [2019-04-17 00:09:44 +0900] [30839] [INFO] Listening at: http://127.0.0.1:8000 (30839) [2019-04-17 00:09:44 +0900] [30839] [INFO] Using worker: sync [2019-04-17 00:09:44 +0900] [30842] [INFO] Booting worker with pid: 30842 application startup!!
åæååŠçãåŒã°ããŸããã
ã¢ã¯ã»ã¹ããŠã¿ãŠããåæååŠçã¯ããåŒã°ããŸããã
$ curl localhost:8000 get response / pid = 30842, thread-name = MainThread
次ã«ãããã»ã¹æ°ã4ãã¹ã¬ããæ°ã2ã«ããŠã¿ãŸãã
$ gunicorn --workers 4 --threads 2 wsgi-app-with-startup:application [2019-04-17 00:10:09 +0900] [31224] [INFO] Starting gunicorn 19.9.0 [2019-04-17 00:10:09 +0900] [31224] [INFO] Listening at: http://127.0.0.1:8000 (31224) [2019-04-17 00:10:09 +0900] [31224] [INFO] Using worker: threads [2019-04-17 00:10:09 +0900] [31227] [INFO] Booting worker with pid: 31227 application startup!! [2019-04-17 00:10:09 +0900] [31228] [INFO] Booting worker with pid: 31228 application startup!! [2019-04-17 00:10:09 +0900] [31229] [INFO] Booting worker with pid: 31229 application startup!! [2019-04-17 00:10:09 +0900] [31230] [INFO] Booting worker with pid: 31230 application startup!!
åæååŠçã4ååŒã°ããŸããâŠã
ã¢ã¯ã»ã¹ããŠã¿ãŠããããããåæååŠçãå床åŒã°ããããšã¯ãªãã®ã§ããâŠã
$ curl localhost:8000 get response / pid = 31227, thread-name = ThreadPoolExecutor-0_0
uWSGI
ç¶ããŠãuWSGIã
ããã©ã«ãç¶æ ã§èµ·åããŠã¿ãŸãã
$ uwsgi --http :8000 --wsgi-file wsgi-app-with-startup.py ... *** Operational MODE: single process *** application startup!! WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x557b141ec140 pid: 31884 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 31884, cores: 1)
åæååŠçã¯åŒã³åºãããŸãããã
ã¢ã¯ã»ã¹ããŠããåæååŠçã¯ããåŒã³åºãããŸããã
$ curl localhost:8000 get response / pid = 31884, thread-name = uWSGIWorker1Core0
ã§ã¯ãããã»ã¹æ°ã4ãã¹ã¬ããæ°ã2ã«ããŠã¿ãŸãã
$ uwsgi --http :8000 --master --processes 4 --threads 2 --wsgi-file wsgi-app-with-startup.py ... *** Operational MODE: preforking+threaded *** application startup!! WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x5612b14c5270 pid: 32534 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 32534) spawned uWSGI worker 1 (pid: 32535, cores: 2) spawned uWSGI worker 2 (pid: 32536, cores: 2) spawned uWSGI worker 3 (pid: 32537, cores: 2) spawned uWSGI worker 4 (pid: 32538, cores: 2) spawned uWSGI http 1 (pid: 32539)
ãããšãuWSGIã®å Žåã¯åæååŠçã1åããåŒã³åºãããŸããã§ããã
ã¢ã¯ã»ã¹ããŠããåæååŠçã¯åŒã°ãããããŸããã
$ curl localhost:8000 get response / pid = 32535, thread-name = uWSGIWorker1Core0
GunicornãšuWSGIãšããWSGIãµãŒããŒã§ããæåã®é¢ã§ããããéããããããã§ããã
ãããªããšãã²ãšã€ã®ã¢ããªã±ãŒã·ã§ã³å
ã§ã€ã³ã¹ã¿ã³ã¹ã®æ°ãå¶éããããšãåæã«ããŠãããã®ãšãã
ã¢ããªã±ãŒã·ã§ã³å
ã§åãã€ã³ã¹ã¿ã³ã¹ãèŠãŠããããšãåæã«ãªã£ãŠãããã®ãšãããã£ããã
ã©ããªããã ãããšãããããæ°ã«ãªããŸãã
䜿ãæã«ã¯ãæ°ã«ãªããšãããã¡ãããšç¢ºèªããæ¹ãè¯ãããã§ããâŠã