The zaqar.openstack.common.lockutils module

FileLock

alias of _FcntlLock

InterProcessLock

alias of _PosixLock

external_lock(name, lock_file_prefix=None, lock_path=None)
internal_lock(name)
lock(*args, **kwds)

Context based lock

This function yields a threading.Semaphore instance (if we don’t use eventlet.monkey_patch(), else semaphore.Semaphore) unless external is True, in which case, it’ll yield an InterProcessLock instance.

Parameters:
  • lock_file_prefix – The lock_file_prefix argument is used to provide lock files on disk with a meaningful prefix.
  • external – The external keyword argument denotes whether this lock should work across multiple processes. This means that if two different workers both run a method decorated with @synchronized(‘mylock’, external=True), only one of them will execute at a time.
main(argv)

Create a dir for locks and pass it to command from arguments

If you run this: python -m openstack.common.lockutils python setup.py testr <etc>

a temporary directory will be created for all your locks and passed to all your tests in an environment variable. The temporary dir will be deleted afterwards and the return value will be preserved.

remove_external_lock_file(name, lock_file_prefix=None)

Remove an external lock file when it’s not used anymore This will be helpful when we have a lot of lock files

set_defaults(lock_path)
synchronized(name, lock_file_prefix=None, external=False, lock_path=None)

Synchronization decorator.

Decorating a method like so:

@synchronized('mylock')
def foo(self, *args):
   ...

ensures that only one thread will execute the foo method at a time.

Different methods can share the same lock:

@synchronized('mylock')
def foo(self, *args):
   ...

@synchronized('mylock')
def bar(self, *args):
   ...

This way only one of either foo or bar can be executing at a time.

synchronized_with_prefix(lock_file_prefix)

Partial object generator for the synchronization decorator.

Redefine @synchronized in each project like so:

(in nova/utils.py)
from nova.openstack.common import lockutils

synchronized = lockutils.synchronized_with_prefix('nova-')


(in nova/foo.py)
from nova import utils

@utils.synchronized('mylock')
def bar(self, *args):
   ...

The lock_file_prefix argument is used to provide lock files on disk with a meaningful prefix.

Previous topic

The zaqar.openstack.common.gettextutils module

Next topic

The zaqar.openstack.common.timeutils module

Project Source

This Page