@@ -30,8 +30,8 @@ def url(self):
30
30
return storage .staticfiles_storage .url (self .path )
31
31
32
32
33
- # This will collect the StaticFile instances across threads.
34
- used_static_files = ContextVar ( "djdt_static_used_static_files" , default = [])
33
+ # This will record and map the StaticFile instances with its associated
34
+ # request across threads and async concurrent requests state.
35
35
request_id_context_var = ContextVar ("djdt_request_id_store" )
36
36
record_static_file_signal = Signal ()
37
37
@@ -99,23 +99,26 @@ def __init__(self, *args, **kwargs):
99
99
self .used_paths = []
100
100
self .request_id = str (uuid .uuid4 ())
101
101
102
+ @classmethod
103
+ def ready (cls ):
104
+ storage .staticfiles_storage = DebugConfiguredStorage ()
105
+
102
106
def _store_static_files_signal_handler (self , sender , staticfile , ** kwargs ):
103
- with contextlib .suppress (LookupError ):
104
- # Only record the static file if the request_id matches the one
105
- # that was used to create the panel.
106
- # as sender of the signal and this handler will have multiple
107
- # concurrent connections and we want to avoid storing of same
108
- # staticfile from other connections as well.
109
- if request_id_context_var .get () == self .request_id :
110
- used_static_files .get ().append (staticfile )
107
+ # Only record the static file if the request_id matches the one
108
+ # that was used to create the panel.
109
+ # as sender of the signal and this handler will have multiple
110
+ # concurrent connections and we want to avoid storing of same
111
+ # staticfile from other connections as well.
112
+ if request_id_context_var .get () == self .request_id :
113
+ self .used_paths .append (staticfile )
111
114
112
115
def enable_instrumentation (self ):
113
- storage .staticfiles_storage = DebugConfiguredStorage ()
114
116
record_static_file_signal .connect (self ._store_static_files_signal_handler )
115
- request_id_context_var .set (self .request_id )
117
+ self . ctx_token = request_id_context_var .set (self .request_id )
116
118
117
119
def disable_instrumentation (self ):
118
120
record_static_file_signal .disconnect (self ._store_static_files_signal_handler )
121
+ request_id_context_var .reset (self .ctx_token )
119
122
120
123
@property
121
124
def num_used (self ):
@@ -131,20 +134,7 @@ def nav_subtitle(self):
131
134
"%(num_used)s file used" , "%(num_used)s files used" , num_used
132
135
) % {"num_used" : num_used }
133
136
134
- # def process_request(self, request):
135
- # reset_token = used_static_files.set([])
136
- # response = super().process_request(request)
137
- # # Make a copy of the used paths so that when the
138
- # # ContextVar is reset, our panel still has the data.
139
- # self.used_paths = used_static_files.get().copy()
140
- # # Reset the ContextVar to be empty again, removing the reference
141
- # # to the list of used files.
142
- # used_static_files.reset(reset_token)
143
- # return response
144
-
145
137
def generate_stats (self , request , response ):
146
- self .used_paths = used_static_files .get ().copy ()
147
- used_static_files .get ().clear ()
148
138
self .record_stats (
149
139
{
150
140
"num_found" : self .num_found ,
0 commit comments