Django Middlewareのメンバー変数(クラス変数?)がリクエストをまたいで保持されていた

ウェブアプリは、1リクエストごとに別のプロセスが生成されて、変数は共有されないと思っていたけど、
Django Middlewareの変数は、リクエスト間で共有されているっぽい。
あとで仕組みを調べようと思う。

https://docs.djangoproject.com/en/1.9/topics/http/middleware/

https://docs.djangoproject.com/en/2.1/topics/http/middleware/

class SimpleMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

memo

class Middleware1(object):

    _count_test = 1

    def __init__(self):
        # One-time configuration and initialization.
        print "Middleware1: __init__()"

    def process_response(self, request, response):
        """
        viewを実行した後に呼びだされるフック。
        """
        print "Middleware1:" + str(self._count_test)
        self._count_test = self._count_test + 1
        return response

settings.py

MIDDLEWARE_CLASSES = (
  
  "apps.test1.middleware.Middleware1",

)

PyCharm WebServerコンソールログ

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Middleware1: __init__()
Middleware1:1
[07/Sep/2018 15:29:27] "GET / HTTP/1.1" 200 1015
Middleware1:2
[07/Sep/2018 15:29:28] "GET /xxx HTTP/1.1" 200 182
Middleware1:3
[07/Sep/2018 15:29:28] "GET /xxx HTTP/1.1" 200 70
Middleware1:4
[07/Sep/2018 15:29:28] "GET /xxx HTTP/1.1" 200 571
Middleware1:5
[07/Sep/2018 15:29:28] "GET /xxx HTTP/1.1" 200 163
Middleware1:6
[07/Sep/2018 15:29:29] "GET /xxx HTTP/1.1" 200 2530
Middleware1:7
[07/Sep/2018 15:29:29] "GET /xxx HTTP/1.1" 200 85
Middleware1:8
[07/Sep/2018 15:29:29] "GET /xxx HTTP/1.1" 200 0
Middleware1:9
[07/Sep/2018 15:29:29] "GET /xxx HTTP/1.1" 200 581
[07/Sep/2018 15:29:29] "GET /xxx HTTP/1.1" 200 5016
Middleware1:10
Middleware1:11
[07/Sep/2018 15:29:29] "GET /xxx HTTP/1.1" 200 53882
[07/Sep/2018 15:29:33] "GET /xxx HTTP/1.1" 200 9004
Middleware1:12
[07/Sep/2018 15:29:33] "GET /xxx HTTP/1.1" 200 9004
Middleware1:13
Middleware1:14
[07/Sep/2018 15:29:33] "GET /xxx HTTP/1.1" 200 9004