Ticket #5632: dbmultithreaded3.diff

File dbmultithreaded3.diff, 6.3 KB (added by teepark, 17 years ago)

based on SVN trunk r7364

  • django/db/models/base.py

     
    66from django.db.models.fields.related import OneToOneRel, ManyToOneRel
    77from django.db.models.query import delete_objects
    88from django.db.models.options import Options, AdminOptions
    9 from django.db import connection, transaction
     9from django.db import connection, transaction, mutex
    1010from django.db.models import signals
    1111from django.db.models.loading import register_models, get_model
    1212from django.dispatch import dispatcher
     
    275275        dispatcher.send(signal=signals.post_save, sender=self.__class__,
    276276                        instance=self, created=(not record_exists), raw=raw)
    277277
     278    save = mutex(save)
    278279    save.alters_data = True
    279280
    280281    def validate(self):
     
    334335        # Actually delete the objects
    335336        delete_objects(seen_objs)
    336337
     338    delete = mutex(delete)
    337339    delete.alters_data = True
    338340
    339341    def _get_FIELD_display(self, field):
  • django/db/models/fields/related.py

     
    1 from django.db import connection, transaction
     1from django.db import connection, transaction, mutex
    22from django.db.models import signals, get_model
    33from django.db.models.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, get_ul_class
    44from django.db.models.related import RelatedObject
     
    254254                for obj in objs:
    255255                    setattr(obj, rel_field.name, instance)
    256256                    obj.save()
     257            add = mutex(add)
    257258            add.alters_data = True
    258259
    259260            def create(self, **kwargs):
    260261                new_obj = self.model(**kwargs)
    261262                self.add(new_obj)
    262263                return new_obj
     264            create = mutex(create)
    263265            create.alters_data = True
    264266
    265267            # remove() and clear() are only provided if the ForeignKey can have a value of null.
     
    273275                            obj.save()
    274276                        else:
    275277                            raise rel_field.rel.to.DoesNotExist, "%r is not related to %r." % (obj, instance)
     278                remove = mutex(remove)
    276279                remove.alters_data = True
    277280
    278281                def clear(self):
    279282                    for obj in self.all():
    280283                        setattr(obj, rel_field.name, None)
    281284                        obj.save()
     285                clear = mutex(clear)
    282286                clear.alters_data = True
    283287
    284288        manager = RelatedManager()
     
    325329            # If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table
    326330            if self.symmetrical:
    327331                self._add_items(self.target_col_name, self.source_col_name, *objs)
     332        add = mutex(add)
    328333        add.alters_data = True
    329334
    330335        def remove(self, *objs):
     
    333338            # If this is a symmetrical m2m relation to self, remove the mirror entry in the m2m table
    334339            if self.symmetrical:
    335340                self._remove_items(self.target_col_name, self.source_col_name, *objs)
     341        remove = mutex(remove)
    336342        remove.alters_data = True
    337343
    338344        def clear(self):
     
    341347            # If this is a symmetrical m2m relation to self, clear the mirror entry in the m2m table
    342348            if self.symmetrical:
    343349                self._clear_items(self.target_col_name)
     350        clear = mutex(clear)
    344351        clear.alters_data = True
    345352
    346353        def create(self, **kwargs):
     
    348355            new_obj.save()
    349356            self.add(new_obj)
    350357            return new_obj
     358        create = mutex(create)
    351359        create.alters_data = True
    352360
    353361        def _add_items(self, source_col_name, target_col_name, *objs):
  • django/db/models/query.py

     
    11from django.conf import settings
    2 from django.db import connection, transaction, IntegrityError
     2from django.db import connection, transaction, IntegrityError, mutex
    33from django.db.models.fields import DateField, FieldDoesNotExist
    44from django.db.models import signals, loading
    55from django.dispatch import dispatcher
     
    273273        obj = self.model(**kwargs)
    274274        obj.save()
    275275        return obj
     276    create = mutex(create)
    276277
    277278    def get_or_create(self, **kwargs):
    278279        """
     
    293294                return obj, True
    294295            except IntegrityError, e:
    295296                return self.get(**kwargs), False
     297    get_or_create = mutex(get_or_create)
    296298
    297299    def latest(self, field_name=None):
    298300        """
     
    354356
    355357        # Clear the result cache, in case this QuerySet gets reused.
    356358        self._result_cache = None
     359    delete = mutex(delete)
    357360    delete.alters_data = True
    358361
    359362    ##################################################
  • django/db/__init__.py

     
    11import os
     2import threading
    23from django.conf import settings
    34from django.core import signals
    45from django.core.exceptions import ImproperlyConfigured
     
    6869    from django.db import transaction
    6970    transaction.rollback_unless_managed()
    7071dispatcher.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()
     75def 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
  • AUTHORS

     
    379379    ymasuda@ethercube.com
    380380    Jarek Zgoda <jarek.zgoda@gmail.com>
    381381    Cheng Zhang
     382    Claudio Marinozzi
     383    Travis Parker <travis.parker@gmail.com>
    382384
    383385A big THANK YOU goes to:
    384386
Back to Top