Ticket #5632: dbmultithreaded.diff
File dbmultithreaded.diff, 5.8 KB (added by , 17 years ago) |
---|
-
django/db/models/serializer.py
1 # -*- coding: utf-8 -*- 2 3 # Author: Claudio Marinozzi (alias PyMan) 4 5 from threading import RLock 6 7 class Serializer : 8 9 def __init__(self) : 10 self.rc = 0 11 self.lock = RLock() 12 13 def acquire(self): 14 self.lock.acquire() 15 self.rc += 1 16 17 def release(self): 18 self.rc -= 1 19 self.lock.release() 20 21 __serializer = Serializer() 22 23 #TODO: maybe it's useful the use of a new setting parameter that if setted to True then makes the use of the 24 #Serialized object (multithreaded scenario), False otherwise (single threaded scenario) 25 26 #NOTE: Now the Serializer object it's useless...it could be just used the RLock object itself. 27 28 def mutex(func): 29 def decorator(*args, **kwargs): 30 __serializer.acquire() 31 try : 32 res = func(*args, **kwargs) 33 except : 34 __serializer.release() 35 raise 36 __serializer.release() 37 return res 38 return decorator 39 Nessun a-capo alla fine del file -
django/db/models/base.py
19 19 import sys 20 20 import os 21 21 22 from django.db.models.serializer import mutex 23 22 24 class ModelBase(type): 23 25 "Metaclass for all models" 24 26 def __new__(cls, name, bases, attrs): … … 206 208 207 209 _prepare = classmethod(_prepare) 208 210 211 @mutex 209 212 def save(self, raw=False): 210 213 dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) 211 214 … … 319 322 for sub_obj in getattr(self, rel_opts_name).all(): 320 323 sub_obj._collect_sub_objects(seen_objs) 321 324 325 @mutex 322 326 def delete(self): 323 327 assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname) 324 328 -
django/db/models/fields/related.py
11 11 from django import newforms as forms 12 12 from django.dispatch import dispatcher 13 13 14 from django.db.models.serializer import mutex 15 14 16 try: 15 17 set 16 18 except NameError: … … 214 216 def get_query_set(self): 215 217 return superclass.get_query_set(self).filter(**(self.core_filters)) 216 218 219 @mutex 217 220 def add(self, *objs): 218 221 for obj in objs: 219 222 setattr(obj, rel_field.name, instance) 220 223 obj.save() 221 224 add.alters_data = True 222 225 226 @mutex 223 227 def create(self, **kwargs): 224 228 new_obj = self.model(**kwargs) 225 229 self.add(new_obj) … … 228 232 229 233 # remove() and clear() are only provided if the ForeignKey can have a value of null. 230 234 if rel_field.null: 235 @mutex 231 236 def remove(self, *objs): 232 237 val = getattr(instance, rel_field.rel.get_related_field().attname) 233 238 for obj in objs: … … 239 244 raise rel_field.rel.to.DoesNotExist, "%r is not related to %r." % (obj, instance) 240 245 remove.alters_data = True 241 246 247 @mutex 242 248 def clear(self): 243 249 for obj in self.all(): 244 250 setattr(obj, rel_field.name, None) … … 283 289 def get_query_set(self): 284 290 return superclass.get_query_set(self).filter(**(self.core_filters)) 285 291 292 @mutex 286 293 def add(self, *objs): 287 294 self._add_items(self.source_col_name, self.target_col_name, *objs) 288 295 … … 291 298 self._add_items(self.target_col_name, self.source_col_name, *objs) 292 299 add.alters_data = True 293 300 301 @mutex 294 302 def remove(self, *objs): 295 303 self._remove_items(self.source_col_name, self.target_col_name, *objs) 296 304 … … 299 307 self._remove_items(self.target_col_name, self.source_col_name, *objs) 300 308 remove.alters_data = True 301 309 310 @mutex 302 311 def clear(self): 303 312 self._clear_items(self.source_col_name) 304 313 … … 307 316 self._clear_items(self.target_col_name) 308 317 clear.alters_data = True 309 318 319 @mutex 310 320 def create(self, **kwargs): 311 321 new_obj = self.model(**kwargs) 312 322 new_obj.save() -
django/db/models/query.py
10 10 import operator 11 11 import re 12 12 13 from django.db.models.serializer import mutex 14 13 15 try: 14 16 set 15 17 except NameError: … … 264 266 assert len(obj_list) == 1, "get() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.model._meta.object_name, len(obj_list), kwargs) 265 267 return obj_list[0] 266 268 269 @mutex 267 270 def create(self, **kwargs): 268 271 """ 269 272 Create a new object with the given kwargs, saving it to the database … … 273 276 obj.save() 274 277 return obj 275 278 279 @mutex 276 280 def get_or_create(self, **kwargs): 277 281 """ 278 282 Looks up an object with the given kwargs, creating one if necessary. … … 318 322 qs._params.extend(id_list) 319 323 return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()]) 320 324 325 @mutex 321 326 def delete(self): 322 327 """ 323 328 Deletes the records in the current QuerySet.