@synchronized dect'r &c.

castironpi at gmail.com castironpi at gmail.com
Sat Feb 23 20:47:02 EST 2008


On Feb 23, 6:19 pm, Paul McGuire <pt... at austin.rr.com> wrote:
> On Feb 23, 2:03 pm, castiro... at gmail.com wrote:
>
>
>
>
>
>
>
> > 1)  @synchronized
>
> > @synchronized
> > def function( arg ):
> >    behavior()
>
> > Synchronized prevents usage from more than one caller at once: look up
> > the function in a hash, acquire its lock, and call.
>
> > def synchronized( func ):
> >    def presynch( *ar, **kwar ):
> >       with _synchlock:
> >          lock= _synchhash.setdefault( func, allocate_lock() )
> >          with lock:
> >             return func( *ar, **kwar )
> >    return presynch
>
> No need for a global _synchhash, just hang the function lock on the
> function itself:
>
> def synch(f):
>     f._synchLock = Lock()
>     def synchedFn(*args, **kwargs):
>         with f._synchLock:
>             f(*args, **kwargs)
>     return synchedFn
>
> You might also look at the PythonDecoratorLibrary page of the Python
> wiki, there is a synchronization decorator there that allows the
> function caller to specify its own lock object (in case a single lock
> should be shared across multiple functions).
>
> -- Paul- Hide quoted text -
>
> - Show quoted text -

Why not just:

def synched( f ):
   l= Lock()
   def presynched( *a, **kwa ):
      with l:
          return f( *a, **kwa )

It's not like the lock is ever used anywhere else.  Besides, if it is,
isn't the correct spelling:

class Asynched:
   def __init__( self, func ):
      self.func, self.lock= func, Lock()
   def __call__( self, *a, **kwa ):
      return self.func( *a, **kwa )

and

def synched( func ):
   return Asynched( func )

or even

synched= Asynched

?



More information about the Python-list mailing list