CLOVER🍀

That was when it all began.

WSGIアプリケヌションずプロセス数、スレッド数の関係を芋る

これは、なにをしたくお曞いたもの

先日、WSGIサヌバヌずしお、GunicornやuWSGIを動かしおみたした。

uWSGIを試してみる - CLOVER🍀

Gunicornを試してみる - CLOVER🍀

この時に、これらのサヌバヌには起動時にプロセス数やスレッド数を䞎えるこずができるずわかりたした。

ずころで、耇数プロセスや耇数スレッドにした時に、アプリケヌションから芋たらどういうこずを気にした方が
よさそうなんでしょうかね

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サヌバヌでも、挙動の面でいろいろ違いがありそうですね。

こうなるず、ひず぀のアプリケヌション内でむンスタンスの数が制限するこずを前提にしおいるものずか、
アプリケヌション内で同じむンスタンスを芋おいるこずが前提になっおいるものずかがあったら、
どうなるんだろうずかいろいろ気になりたす。

䜿う時には、気になるずころをちゃんず確認した方が良さそうですね 。