Oslo Concurrency Library

7.4.1 · active · verified Wed Apr 15

The oslo.concurrency library provides utilities for safely running multi-thread and multi-process applications using locking mechanisms, as well as for running external processes. It is a core component of the OpenStack Oslo project, which generally follows a six-month release cadence for major OpenStack releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `oslo_concurrency.lockutils.synchronized` for inter-process locking. It configures a `lock_path` and defines a function that will be synchronized across different 'workers'. Note that for actual multi-process execution, you would typically run separate Python processes that each call `my_locked_function`.

import os
from oslo_concurrency import lockutils

# For inter-process locking, a lock_path must be configured.
# For this example, we'll use a temporary directory.
# In a real application, this should be a secure, dedicated directory.
lock_dir = os.environ.get('OSLO_LOCK_PATH', '/tmp/oslo_locks')
os.makedirs(lock_dir, exist_ok=True)

# Configure oslo.concurrency to use the lock path
# This is typically done via oslo.config in a real OpenStack project,
# but we can set it directly for a quick example.
lockutils.set_defaults(lock_path=lock_dir)

@lockutils.synchronized('my-resource', external=True)
def my_locked_function(worker_id):
    print(f"Worker {worker_id} acquired the lock.")
    # Simulate some work
    import time
    time.sleep(0.5)
    print(f"Worker {worker_id} released the lock.")

if __name__ == '__main__':
    # Example of calling the locked function from multiple 'processes'
    # In a real scenario, these would be separate processes.
    print("Attempting to call my_locked_function from multiple 'workers'...")
    for i in range(3):
        my_locked_function(f"SimulatedWorker-{i}")

    # Clean up the lock directory (optional for demo)
    try:
        os.rmdir(lock_dir)
    except OSError:
        # Directory might not be empty if locks were created
        pass

view raw JSON →