Skip to main content

Safelock gives you simple systemwide multithread, multiprocess, multiprogram locks.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safelock-1.3.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safelock-1.3.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file safelock-1.3.3.tar.gz.

File metadata

  • Download URL: safelock-1.3.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for safelock-1.3.3.tar.gz
Algorithm Hash digest
SHA256 324ebe62ff1432073dbd67ee8fec2c6b09258655b64505d9acff741da2e52efc
MD5 9a3f375e44ad344e6ba1055c2ec93876
BLAKE2b-256 379ab102b08015329cd47c86453f1bd3cb000051d31d4fa64defecbe5573fdd7

See more details on using hashes here.

File details

Details for the file safelock-1.3.3-py3-none-any.whl.

File metadata

  • Download URL: safelock-1.3.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for safelock-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5c74486b6facacc125c605f814e5b0357e222cee770d46e6cc59a38b57c63a42
MD5 c98f1d3d3e716853c5089e67c46b5e86
BLAKE2b-256 827a2666a8b2767cb70889a4583f4d5e97abfee51e13b2dcdfbf7eaeff5a51d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page