0
def synchronized(func):
    """Decorator for storage-access methods, which synchronizes on a threading
    lock. The parent object must have 'is_closed' and '_sync_lock' attributes.
    """

    @wraps(func)
    def synchronized_wrapper(self, *args, **kwargs):
        with self._sync_lock:
           return func(self, *args, **kwargs)

    return synchronized_wrapper

the code is in whoosh/src/util.py,I can't understand the synchronized_wrapper's effect and the parameters in synchronized_wrapper(self, *args, **kwargs) from where. Can anyone give me some pointers?

jgritty
  • 11,660
  • 3
  • 38
  • 60
halostack
  • 993
  • 2
  • 13
  • 19

2 Answers2

0

The @wraps decorator is just syntactic sugar for a function closure with argument forwarding. *args refers to a tuple of positional args, and **kwargs refers to a dict of all keyword args that have been passed to func.

Hence if you had:

def f(foo, bar=None):
    ...

and did:

sync_f = someinst.synchronized(f)
sync_f(a, bar=z)

it would basically be like calling:

f(a, bar=z)

but inside the "with self._sync_lock:" context manager

Preet Kukreti
  • 8,417
  • 28
  • 36
0

Decorating a function causes problems with reflection-based operations, and @wraps is designed to make the wrapped function truly simulate the original function. The link lucemia provided has applicable info.

Community
  • 1
  • 1
acjay
  • 34,571
  • 6
  • 57
  • 100