{"id":6404,"library":"oslo-concurrency","title":"Oslo Concurrency Library","description":"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.","status":"active","version":"7.4.1","language":"en","source_language":"en","source_url":"https://github.com/openstack/oslo.concurrency","tags":["concurrency","locking","multiprocessing","threading","openstack"],"install":[{"cmd":"pip install oslo-concurrency","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides underlying locking primitives.","package":"fasteners","optional":false},{"reason":"General utility functions from the Oslo project.","package":"oslo.utils","optional":false},{"reason":"Internationalization support from the Oslo project.","package":"oslo.i18n","optional":false},{"reason":"Configuration management from the Oslo project, used for options like `lock_path`.","package":"oslo.config","optional":false}],"imports":[{"symbol":"lockutils","correct":"from oslo_concurrency import lockutils"},{"symbol":"processutils","correct":"from oslo_concurrency import processutils"}],"quickstart":{"code":"import os\nfrom oslo_concurrency import lockutils\n\n# For inter-process locking, a lock_path must be configured.\n# For this example, we'll use a temporary directory.\n# In a real application, this should be a secure, dedicated directory.\nlock_dir = os.environ.get('OSLO_LOCK_PATH', '/tmp/oslo_locks')\nos.makedirs(lock_dir, exist_ok=True)\n\n# Configure oslo.concurrency to use the lock path\n# This is typically done via oslo.config in a real OpenStack project,\n# but we can set it directly for a quick example.\nlockutils.set_defaults(lock_path=lock_dir)\n\n@lockutils.synchronized('my-resource', external=True)\ndef my_locked_function(worker_id):\n    print(f\"Worker {worker_id} acquired the lock.\")\n    # Simulate some work\n    import time\n    time.sleep(0.5)\n    print(f\"Worker {worker_id} released the lock.\")\n\nif __name__ == '__main__':\n    # Example of calling the locked function from multiple 'processes'\n    # In a real scenario, these would be separate processes.\n    print(\"Attempting to call my_locked_function from multiple 'workers'...\")\n    for i in range(3):\n        my_locked_function(f\"SimulatedWorker-{i}\")\n\n    # Clean up the lock directory (optional for demo)\n    try:\n        os.rmdir(lock_dir)\n    except OSError:\n        # Directory might not be empty if locks were created\n        pass\n","lang":"python","description":"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`."},"warnings":[{"fix":"Update import statements to directly reference `oslo_concurrency` (e.g., `from oslo_concurrency import lockutils`).","message":"Older `oslo` libraries, including `oslo.concurrency`, deprecated and then removed the use of the `oslo` namespace package. Direct imports like `from oslo_concurrency import lockutils` are now required instead of older patterns that might have relied on a top-level `oslo` package structure.","severity":"breaking","affected_versions":"< 5.0.0 (approx), specific to each oslo library's transition."},{"fix":"Set the `lock_path` via `lockutils.set_defaults(lock_path='/path/to/locks')` or by configuring `oslo_concurrency.lock_path` if using `oslo.config`.","message":"When using inter-process locks (`external=True` in `lockutils.synchronized`), a `lock_path` *must* be configured. This directory is used to store lock files and needs to be accessible and writable by the processes requiring synchronization. For security, it should ideally only be writable by the user running those processes. If not set, `oslo.concurrency` will raise an error or behave unexpectedly.","severity":"gotcha","affected_versions":"All versions using `external=True` locks."},{"fix":"Carefully review the concurrency safety guarantees of all integrated libraries. For `oslo.messaging`, use separate connection instances for concurrent read and write operations if using eventlet/green threads.","message":"While `oslo.concurrency` provides tools for safe concurrency, be aware of how other libraries handle concurrency. For example, `oslo.messaging` connections are explicitly noted as *not* concurrency safe and should not be shared between threads/greenthreads for both reading and writing, which can lead to eventlet complaints if not handled with separate connections for different operations.","severity":"gotcha","affected_versions":"All versions (context-dependent on integration with other libraries)."}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}