{"id":15009780,"url":"https://github.com/wolph/portalocker","last_synced_at":"2025-05-14T03:09:27.713Z","repository":{"id":43040895,"uuid":"1401974","full_name":"wolph/portalocker","owner":"wolph","description":"An easy library for Python file locking. It works on Windows, Linux, BSD and Unix systems and can even perform distributed locking. Naturally it also supports the with statement.","archived":false,"fork":false,"pushed_at":"2025-05-12T21:41:13.000Z","size":527,"stargazers_count":288,"open_issues_count":4,"forks_count":53,"subscribers_count":8,"default_branch":"develop","last_synced_at":"2025-05-12T22:35:21.404Z","etag":null,"topics":["distributed","lock","locking","python"],"latest_commit_sha":null,"homepage":"http://portalocker.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wolph.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"wolph"}},"created_at":"2011-02-23T11:42:47.000Z","updated_at":"2025-05-12T21:41:17.000Z","dependencies_parsed_at":"2024-05-11T11:31:41.456Z","dependency_job_id":"1f65c987-36f5-46c1-becf-25fb10d65f70","html_url":"https://github.com/wolph/portalocker","commit_stats":{"total_commits":344,"total_committers":26,"mean_commits":13.23076923076923,"dds":"0.19186046511627908","last_synced_commit":"025c1989d309d89219fbb848bd47d4ff23429b62"},"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolph%2Fportalocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolph%2Fportalocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolph%2Fportalocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolph%2Fportalocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wolph","download_url":"https://codeload.github.com/wolph/portalocker/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253837373,"owners_count":21971981,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed","lock","locking","python"],"created_at":"2024-09-24T19:28:31.838Z","updated_at":"2025-05-14T03:09:27.694Z","avatar_url":"https://github.com/wolph.png","language":"Python","readme":"############################################\nportalocker - Cross-platform locking library\n############################################\n\n.. image:: https://github.com/WoLpH/portalocker/actions/workflows/python-package.yml/badge.svg?branch=master\n    :alt: Linux Test Status\n    :target: https://github.com/WoLpH/portalocker/actions/\n\n.. image:: https://ci.appveyor.com/api/projects/status/mgqry98hgpy4prhh?svg=true\n    :alt: Windows Tests Status\n    :target: https://ci.appveyor.com/project/WoLpH/portalocker\n\n.. image:: https://coveralls.io/repos/WoLpH/portalocker/badge.svg?branch=master\n    :alt: Coverage Status\n    :target: https://coveralls.io/r/WoLpH/portalocker?branch=master\n\nOverview\n--------\n\nPortalocker is a library to provide an easy API to file locking.\n\nAn important detail to note is that on Linux and Unix systems the locks are\nadvisory by default. By specifying the `-o mand` option to the mount command it\nis possible to enable mandatory file locking on Linux. This is generally not\nrecommended however. For more information about the subject:\n\n - https://en.wikipedia.org/wiki/File_locking\n - http://stackoverflow.com/questions/39292051/portalocker-does-not-seem-to-lock\n - https://stackoverflow.com/questions/12062466/mandatory-file-lock-on-linux\n\nThe module is currently maintained by Rick van Hattem \u003cWolph@wol.ph\u003e.\nThe project resides at https://github.com/WoLpH/portalocker . Bugs and feature\nrequests can be submitted there. Patches are also very welcome.\n\nSecurity contact information\n------------------------------------------------------------------------------\n\nTo report a security vulnerability, please use the\n`Tidelift security contact \u003chttps://tidelift.com/security\u003e`_.\nTidelift will coordinate the fix and disclosure.\n\nRedis Locks\n-----------\n\nThis library now features a lock based on Redis which allows for locks across\nmultiple threads, processes and even distributed locks across multiple\ncomputers.\n\nIt is an extremely reliable Redis lock that is based on pubsub.\n\nAs opposed to most Redis locking systems based on key/value pairs,\nthis locking method is based on the pubsub system. The big advantage is\nthat if the connection gets killed due to network issues, crashing\nprocesses or otherwise, it will still immediately unlock instead of\nwaiting for a lock timeout.\n\nFirst make sure you have everything installed correctly:\n\n::\n\n    pip install \"portalocker[redis]\"\n\nUsage is really easy:\n\n::\n\n    import portalocker\n\n    lock = portalocker.RedisLock('some_lock_channel_name')\n\n    with lock:\n        print('do something here')\n\nThe API is essentially identical to the other ``Lock`` classes so in addition\nto the ``with`` statement you can also use ``lock.acquire(...)``.\n\nPython 2\n--------\n\nPython 2 was supported in versions before Portalocker 2.0. If you are still\nusing\nPython 2,\nyou can run this to install:\n\n::\n\n    pip install \"portalocker\u003c2\"\n\nTips\n----\n\nOn some networked filesystems it might be needed to force a `os.fsync()` before\nclosing the file so it's actually written before another client reads the file.\nEffectively this comes down to:\n\n::\n\n   with portalocker.Lock('some_file', 'rb+', timeout=60) as fh:\n       # do what you need to do\n       ...\n\n       # flush and sync to filesystem\n       fh.flush()\n       os.fsync(fh.fileno())\n\nLinks\n-----\n\n* Documentation\n    - http://portalocker.readthedocs.org/en/latest/\n* Source\n    - https://github.com/WoLpH/portalocker\n* Bug reports\n    - https://github.com/WoLpH/portalocker/issues\n* Package homepage\n    - https://pypi.python.org/pypi/portalocker\n* My blog\n    - http://w.wol.ph/\n\nExamples\n--------\n\nTo make sure your cache generation scripts don't race, use the `Lock` class:\n\n\u003e\u003e\u003e import portalocker\n\u003e\u003e\u003e with portalocker.Lock('somefile', timeout=1) as fh:\n...     print('writing some stuff to my cache...', file=fh)\n\nTo customize the opening and locking a manual approach is also possible:\n\n\u003e\u003e\u003e import portalocker\n\u003e\u003e\u003e file = open('somefile', 'r+')\n\u003e\u003e\u003e portalocker.lock(file, portalocker.LockFlags.EXCLUSIVE)\n\u003e\u003e\u003e file.seek(12)\n\u003e\u003e\u003e file.write('foo')\n\u003e\u003e\u003e file.close()\n\nExplicitly unlocking is not needed in most cases but omitting it has been known\nto cause issues:\nhttps://github.com/AzureAD/microsoft-authentication-extensions-for-python/issues/42#issuecomment-601108266\n\nIf needed, it can be done through:\n\n\u003e\u003e\u003e portalocker.unlock(file)\n\nDo note that your data might still be in a buffer so it is possible that your\ndata is not available until you `flush()` or `close()`.\n\nTo create a cross platform bounded semaphore across multiple processes you can\nuse the `BoundedSemaphore` class which functions somewhat similar to\n`threading.BoundedSemaphore`:\n\n\u003e\u003e\u003e import portalocker\n\u003e\u003e\u003e n = 2\n\u003e\u003e\u003e timeout = 0.1\n\n\u003e\u003e\u003e semaphore_a = portalocker.BoundedSemaphore(n, timeout=timeout)\n\u003e\u003e\u003e semaphore_b = portalocker.BoundedSemaphore(n, timeout=timeout)\n\u003e\u003e\u003e semaphore_c = portalocker.BoundedSemaphore(n, timeout=timeout)\n\n\u003e\u003e\u003e semaphore_a.acquire()\n\u003cportalocker.utils.Lock object at ...\u003e\n\u003e\u003e\u003e semaphore_b.acquire()\n\u003cportalocker.utils.Lock object at ...\u003e\n\u003e\u003e\u003e semaphore_c.acquire()\nTraceback (most recent call last):\n  ...\nportalocker.exceptions.AlreadyLocked\n\n\nMore examples can be found in the\n`tests \u003chttp://portalocker.readthedocs.io/en/latest/_modules/tests/tests.html\u003e`_.\n\n\nVersioning\n----------\n\nThis library follows `Semantic Versioning \u003chttp://semver.org/\u003e`_.\n\n\nChangelog\n---------\n\nEvery release has a ``git tag`` with a commit message for the tag\nexplaining what was added and/or changed. The list of tags/releases\nincluding the commit messages can be found here:\nhttps://github.com/WoLpH/portalocker/releases\n\nLicense\n-------\n\nSee the `LICENSE \u003chttps://github.com/WoLpH/portalocker/blob/develop/LICENSE\u003e`_ file.\n\n","funding_links":["https://github.com/sponsors/wolph","https://tidelift.com/security"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwolph%2Fportalocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwolph%2Fportalocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwolph%2Fportalocker/lists"}