Ticket #5632: dbmultithreaded.diff

File dbmultithreaded.diff, 5.8 KB (added by claudio.marinozzi@…, 17 years ago)

diff created with Django on Trunk at h12pm Italy time

  • django/db/models/serializer.py

     
     1# -*- coding: utf-8 -*-
     2
     3# Author: Claudio Marinozzi (alias PyMan)
     4
     5from threading import RLock
     6
     7class 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
     28def 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

     
    1919import sys
    2020import os
    2121
     22from django.db.models.serializer import mutex
     23
    2224class ModelBase(type):
    2325    "Metaclass for all models"
    2426    def __new__(cls, name, bases, attrs):
     
    206208
    207209    _prepare = classmethod(_prepare)
    208210
     211    @mutex
    209212    def save(self, raw=False):
    210213        dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self)
    211214
     
    319322                for sub_obj in getattr(self, rel_opts_name).all():
    320323                    sub_obj._collect_sub_objects(seen_objs)
    321324
     325    @mutex
    322326    def delete(self):
    323327        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)
    324328
  • django/db/models/fields/related.py

     
    1111from django import newforms as forms
    1212from django.dispatch import dispatcher
    1313
     14from django.db.models.serializer import mutex
     15
    1416try:
    1517    set
    1618except NameError:
     
    214216            def get_query_set(self):
    215217                return superclass.get_query_set(self).filter(**(self.core_filters))
    216218
     219            @mutex
    217220            def add(self, *objs):
    218221                for obj in objs:
    219222                    setattr(obj, rel_field.name, instance)
    220223                    obj.save()
    221224            add.alters_data = True
    222225
     226            @mutex
    223227            def create(self, **kwargs):
    224228                new_obj = self.model(**kwargs)
    225229                self.add(new_obj)
     
    228232
    229233            # remove() and clear() are only provided if the ForeignKey can have a value of null.
    230234            if rel_field.null:
     235                @mutex
    231236                def remove(self, *objs):
    232237                    val = getattr(instance, rel_field.rel.get_related_field().attname)
    233238                    for obj in objs:
     
    239244                            raise rel_field.rel.to.DoesNotExist, "%r is not related to %r." % (obj, instance)
    240245                remove.alters_data = True
    241246
     247                @mutex
    242248                def clear(self):
    243249                    for obj in self.all():
    244250                        setattr(obj, rel_field.name, None)
     
    283289        def get_query_set(self):
    284290            return superclass.get_query_set(self).filter(**(self.core_filters))
    285291
     292        @mutex
    286293        def add(self, *objs):
    287294            self._add_items(self.source_col_name, self.target_col_name, *objs)
    288295
     
    291298                self._add_items(self.target_col_name, self.source_col_name, *objs)
    292299        add.alters_data = True
    293300
     301        @mutex
    294302        def remove(self, *objs):
    295303            self._remove_items(self.source_col_name, self.target_col_name, *objs)
    296304
     
    299307                self._remove_items(self.target_col_name, self.source_col_name, *objs)
    300308        remove.alters_data = True
    301309
     310        @mutex
    302311        def clear(self):
    303312            self._clear_items(self.source_col_name)
    304313
     
    307316                self._clear_items(self.target_col_name)
    308317        clear.alters_data = True
    309318
     319        @mutex
    310320        def create(self, **kwargs):
    311321            new_obj = self.model(**kwargs)
    312322            new_obj.save()
  • django/db/models/query.py

     
    1010import operator
    1111import re
    1212
     13from django.db.models.serializer import mutex
     14
    1315try:
    1416    set
    1517except NameError:
     
    264266        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)
    265267        return obj_list[0]
    266268
     269    @mutex
    267270    def create(self, **kwargs):
    268271        """
    269272        Create a new object with the given kwargs, saving it to the database
     
    273276        obj.save()
    274277        return obj
    275278
     279    @mutex
    276280    def get_or_create(self, **kwargs):
    277281        """
    278282        Looks up an object with the given kwargs, creating one if necessary.
     
    318322        qs._params.extend(id_list)
    319323        return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
    320324
     325    @mutex
    321326    def delete(self):
    322327        """
    323328        Deletes the records in the current QuerySet.
Back to Top