{"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":"python","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,"search_vec":"'applic':19 'cadenc':48 'compon':34 'concurr':2,53 'core':33 'extern':28 'follow':42 'general':41 'librari':3,6 'lock':21,54 'major':50 'mechan':22 'month':46 'multi':13,17 'multi-process':16 'multi-thread':12 'multiprocess':55 'openstack':37,51,57 'oslo':1,38 'oslo.concurrency':5 'process':18,29 'project':39 'provid':7 'releas':47,52 'run':11,27 'safe':10 'six':45 'six-month':44 'thread':14,56 'use':20 'util':8 'well':24","created_at":"2026-04-15T05:35:49.606094+00:00","updated_at":"2026-04-16T17:49:20.310198+00:00","problems":[{"fix":"Install the library using pip: `pip install oslo-concurrency`","cause":"The `oslo.concurrency` package is not installed in the Python environment, or the environment where the code is being run does not have access to the installed package.","error":"ImportError: No module named oslo_concurrency"},{"fix":"Examine the `stdout` and `stderr` attributes of the `ProcessExecutionError` exception for details on why the command failed. Ensure the command syntax is correct, its dependencies are met, and it runs successfully outside of the `oslo-concurrency` context. You can also specify allowed exit codes using the `check_exit_code` parameter in `execute` or `trycmd`.","cause":"This error occurs when `oslo_concurrency.processutils.execute` or `trycmd` attempts to run an external command, and the command exits with a non-zero status code (indicating an error) or produces unexpected output.","error":"ProcessExecutionError: Unexpected error while running command. Command: <command_details> Exit code: <N>"},{"fix":"Configure a `lock_path` globally using `lockutils.set_defaults(lock_path='/path/to/lock/files')` before using the decorator, or ensure the `OSLO_LOCK_PATH` environment variable is set. The specified directory must be writable by the user running the processes.","cause":"When `external=True` is set for `lockutils.synchronized`, `oslo.concurrency` requires a designated directory for lock files to enable inter-process locking. If `lock_path` is not configured, the external locking mechanism cannot function correctly.","error":"When using `@lockutils.synchronized('mylock', external=True)` my application fails or does not acquire a lock."},{"fix":"Review the lock acquisition and release logic to ensure consistent ordering and proper error handling that guarantees lock release. If using `multiprocessing`, consider setting the start method to 'spawn' or 'forkserver' (e.g., `multiprocessing.set_start_method('spawn', force=True)` at the beginning of your main script) to avoid issues related to inherited resources from the parent process.","cause":"While `oslo.concurrency` provides robust locking, deadlocks can still occur due to incorrect usage patterns, such as acquiring locks in inconsistent orders across different processes or threads, or failing to release a lock after an error. Issues with Python's `multiprocessing` library's default 'fork' start method on some systems can also contribute.","error":"My application hangs or experiences deadlocks when using oslo.concurrency locks with multiprocessing."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"7.5.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://docs.openstack.org/oslo.concurrency","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/oslo-concurrency/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}