The cinder.openstack.common.lockutils Module

InterProcessLock

alias of _PosixLock

set_defaults(lock_path)
synchronized(name, lock_file_prefix, 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.

Parameters:lock_file_prefix – The lock_file_prefix argument is used to provide

lock files on disk with a meaningful prefix. The prefix should end with a hyphen (‘-‘) if specified.

Parameters:external – The external keyword argument denotes whether this lock

should work across multiple processes. This means that if two different workers both run a a method decorated with @synchronized(‘mylock’, external=True), only one of them will execute at a time.

Parameters:lock_path – The lock_path keyword argument is used to specify a

special location for external lock files to live. If nothing is set, then CONF.lock_path is used as a default.

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. The prefix should end with a hyphen (‘-‘) if specified.

Previous topic

The cinder.openstack.common.local Module

Next topic

The cinder.openstack.common.log Module

This Page