Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Cross-platform locks for threads and processes.
pip install fasteners
Lock for processes has the same API as the threading.Lock for threads:
import fasteners
import threading
lock = threading.Lock() # for threads
lock = fasteners.InterProcessLock('path/to/lock.file') # for processes
with lock:
... # exclusive access
# or alternatively
lock.acquire()
... # exclusive access
lock.release()
Reader Writer lock has a similar API, which is the same for threads or processes:
import fasteners
rw_lock = fasteners.ReaderWriterLock() # for threads
rw_lock = fasteners.InterProcessReaderWriterLock('path/to/lock.file') # for processes
with rw_lock.write_lock():
... # write access
with rw_lock.read_lock():
... # read access
# or alternatively
rw_lock.acquire_read_lock()
... # read access
rw_lock.release_read_lock()
rw_lock.acquire_write_lock()
... # write access
rw_lock.release_write_lock()
Python standard library provides a lock for threads (both a reentrant one, and a non-reentrant one, see below). Fasteners extends this, and provides a lock for processes, as well as Reader Writer locks for both threads and processes. Definitions of terms used in this overview can be found in the glossary.
The specifics of the locks are as follows:
The fasteners.InterProcessLock
uses fcntl on Unix-like systems and
msvc _locking on Windows.
As a result, if used cross-platform it guarantees an intersection of their features:
lock | reentrant | mandatory |
---|---|---|
fcntl | ✘ | ✘ |
_locking | ✔ | ✔ |
fasteners.InterProcessLock | ✘ | ✘ |
The fasteners.InterProcessReaderWriterLock
also uses fcntl on Unix-like systems and
LockFileEx on Windows. Their
features are as follows:
lock | reentrant | mandatory | upgradable | preference |
---|---|---|---|---|
fcntl | ✘ | ✘ | ✔ | reader |
LockFileEx | ✔ | ✔ | ✘ | reader |
fasteners.InterProcessReaderWriterLock | ✘ | ✘ | ✘ | reader |
Fasteners does not provide a simple thread lock, but for the sake of comparison note that the threading
module
provides both a reentrant and non-reentrant locks:
lock | reentrant | mandatory |
---|---|---|
threading.Lock | ✘ | ✘ |
threading.RLock | ✔ | ✘ |
The fasteners.ReaderWriterLock
at the moment is as follows:
lock | reentrant | mandatory | upgradable | preference |
---|---|---|---|---|
fasteners.ReaderWriterLock | ✔ | ✘ | ✘ | writer |
If your threads are created by some other means than the standard library threading
module (for example eventlet
), you may need to provide the corresponding thread
identification and synchronisation functions to the ReaderWriterLock
.
FAQs
A python package that provides useful locks
We found that fasteners demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.