Skip to main content

Safelock

Mutually exclusive locks are usually the simplest way to get safe concurrent access to a shared resource. But implementing the locks is a bear to get right. Get simple systemwide multithread, multiprocess, and multiprogram safe locks.

Description

Multithreading Concurrency in Python Solved

When you first try python multithreading and access a database, it usually works. That's because databases have some concurrency built in. With any other shared resource, your program frequently crashes. Concurrency is hard. Safelock makes it easy.

Start as many threads, subprocesses, or programs as you like. When they try to access a shared resource, Safelock only lets one run at a time.

Install

sudo pip3 install safelock

If you are installing Safelock from codeberg.org, be sure to also install the "safelog" package.

Updates

All future updates will only be available from:

git clone https://codeberg.org/topdevpros/safelock.git

How it Works

Safelock includes a small server that must run as root. See Configuration to learn how to start the server.

In any python code that you want locked,

from solidlibs.os.lock import locked

with locked():
    ... your locked code ...

It's that easy.

You access the shared resource only in your locked code. No matter how many threads, subprocesses, or separate programs enter your locked code, Safelock only lets one run at a time. So only one job gets the resource at a time.

Safelock manages python multithreading concurrency

No more crashes caused by resource conflict.

Complex apps frequently require access to the same resources at the same time. This can cause serious contention which slows down an app or crashes it. The python standard library provides some resource sharing, but it's very hard to get it right. Safelock makes it simple.

Safelock retries the lock automatically for you. If the lock times out, your app must decide whether to try again.

If you need to access a shared resource from multiple places in your code, always get the lock for that resource from a single function. Example:

    def my_locked_resource():
        return locked():

Then every time you need access to the shared resource:

    with my_locked_resource():
        ... your locked code ...

Using syncronized locks is usually the simplest way to get safe concurrent access to a shared resource. But implementing the locks is a bear to get right. Get simple systemwide multithread, multiprocess, and multiprogram safe locks.

Safelock automatically creates and manages a separate system-wide lock for every locked context block.

Configuration

Safelock uses Safelog. Both are installed when you install Safelock regardless which install method you use. Both include small servers to manage system-wide access.

The safelock and safelog commands installed in /usr/bin. The safelock and safelog servers must run as root.

You can start the servers automatically with systemd. Get the service files:

safelock.service
safelog.service

Then:

    systemctl enable MYPATH/safelog.service
    systemctl start safelog
    systemctl enable MYPATH/safelock.service
    systemctl start safelock

MYPATH is the directory where you saved the service files.

If you don't use systemd, start both servers by hand:

    safelog &
    safelock &

You're ready to use safelock.

Manage your python multithreading concurrency with simple locks. Get Safelock.

Release files for safelock 1.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for safelock 1.3.3
File Size Uploaded
safelock-1.3.3.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for safelock 1.3.3
File Interpreter ABI Platform
safelock-1.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / safelock-1.3.3.tar.gz

Download URL safelock-1.3.3.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
324ebe62ff1432073dbd67ee8fec2c6b09258655b64505d9acff741da2e52efc
BLAKE2b-256 checksum
How to use checksums
379ab102b08015329cd47c86453f1bd3cb000051d31d4fa64defecbe5573fdd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / safelock-1.3.3-py3-none-any.whl

Download URL safelock-1.3.3-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c74486b6facacc125c605f814e5b0357e222cee770d46e6cc59a38b57c63a42
BLAKE2b-256 checksum
How to use checksums
827a2666a8b2767cb70889a4583f4d5e97abfee51e13b2dcdfbf7eaeff5a51d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.3.3 This release

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2

2 release files

1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page