Ticket #5632: dbmultithreaded2.diff
File dbmultithreaded2.diff, 5.9 KB (added by , 17 years ago) |
---|
-
django/db/models/base.py
6 6 from django.db.models.fields.related import OneToOneRel, ManyToOneRel 7 7 from django.db.models.query import delete_objects 8 8 from django.db.models.options import Options, AdminOptions 9 from django.db import connection, transaction 9 from django.db import connection, transaction, mutex 10 10 from django.db.models import signals 11 11 from django.db.models.loading import register_models, get_model 12 12 from django.dispatch import dispatcher … … 275 275 dispatcher.send(signal=signals.post_save, sender=self.__class__, 276 276 instance=self, created=(not record_exists), raw=raw) 277 277 278 save = mutex(save) 278 279 save.alters_data = True 279 280 280 281 def validate(self): … … 334 335 # Actually delete the objects 335 336 delete_objects(seen_objs) 336 337 338 delete = mutex(delete) 337 339 delete.alters_data = True 338 340 339 341 def _get_FIELD_display(self, field): -
django/db/models/fields/related.py
1 from django.db import connection, transaction 1 from django.db import connection, transaction, mutex 2 2 from django.db.models import signals, get_model 3 3 from django.db.models.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, get_ul_class 4 4 from django.db.models.related import RelatedObject … … 254 254 for obj in objs: 255 255 setattr(obj, rel_field.name, instance) 256 256 obj.save() 257 add = mutex(add) 257 258 add.alters_data = True 258 259 259 260 def create(self, **kwargs): 260 261 new_obj = self.model(**kwargs) 261 262 self.add(new_obj) 262 263 return new_obj 264 create = mutex(create) 263 265 create.alters_data = True 264 266 265 267 # remove() and clear() are only provided if the ForeignKey can have a value of null. … … 273 275 obj.save() 274 276 else: 275 277 raise rel_field.rel.to.DoesNotExist, "%r is not related to %r." % (obj, instance) 278 remove = mutex(remove) 276 279 remove.alters_data = True 277 280 278 281 def clear(self): 279 282 for obj in self.all(): 280 283 setattr(obj, rel_field.name, None) 281 284 obj.save() 285 clear = mutex(clear) 282 286 clear.alters_data = True 283 287 284 288 manager = RelatedManager() … … 325 329 # If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table 326 330 if self.symmetrical: 327 331 self._add_items(self.target_col_name, self.source_col_name, *objs) 332 add = mutex(add) 328 333 add.alters_data = True 329 334 330 335 def remove(self, *objs): … … 333 338 # If this is a symmetrical m2m relation to self, remove the mirror entry in the m2m table 334 339 if self.symmetrical: 335 340 self._remove_items(self.target_col_name, self.source_col_name, *objs) 341 remove = mutex(remove) 336 342 remove.alters_data = True 337 343 338 344 def clear(self): … … 341 347 # If this is a symmetrical m2m relation to self, clear the mirror entry in the m2m table 342 348 if self.symmetrical: 343 349 self._clear_items(self.target_col_name) 350 clear = mutex(clear) 344 351 clear.alters_data = True 345 352 346 353 def create(self, **kwargs): … … 348 355 new_obj.save() 349 356 self.add(new_obj) 350 357 return new_obj 358 create = mutex(create) 351 359 create.alters_data = True 352 360 353 361 def _add_items(self, source_col_name, target_col_name, *objs): -
django/db/models/query.py
1 1 from django.conf import settings 2 from django.db import connection, transaction, IntegrityError 2 from django.db import connection, transaction, IntegrityError, mutex 3 3 from django.db.models.fields import DateField, FieldDoesNotExist 4 4 from django.db.models import signals, loading 5 5 from django.dispatch import dispatcher … … 273 273 obj = self.model(**kwargs) 274 274 obj.save() 275 275 return obj 276 create = mutex(create) 276 277 277 278 def get_or_create(self, **kwargs): 278 279 """ … … 293 294 return obj, True 294 295 except IntegrityError, e: 295 296 return self.get(**kwargs), False 297 get_or_create = mutex(get_or_create) 296 298 297 299 def latest(self, field_name=None): 298 300 """ … … 354 356 355 357 # Clear the result cache, in case this QuerySet gets reused. 356 358 self._result_cache = None 359 delete = mutex(delete) 357 360 delete.alters_data = True 358 361 359 362 ################################################## -
django/db/__init__.py
1 1 import os 2 import threading 2 3 from django.conf import settings 3 4 from django.core import signals 4 5 from django.core.exceptions import ImproperlyConfigured … … 68 69 from django.db import transaction 69 70 transaction.rollback_unless_managed() 70 71 dispatcher.connect(_rollback_on_exception, signal=signals.got_request_exception) 72 73 # Decorate data-modifying db functions to use a threading.RLock 74 __lock = threading.RLock() 75 def mutex(func): 76 def decorated(*args, **kwargs): 77 __lock.acquire() 78 try: 79 func(*args, **kwargs) 80 except: 81 __lock.release() 82 raise 83 __lock.release() 84 return decorated 85