Skip to main content

pylocked provides utility classes (and functions) to lock any resource (or function) and thus make them thread and / or concurrency safe.

Project description

pylocked provides utility classes (and functions) to lock any resource (or function) and thus make them thread and / or concurrency safe.

Install

From pip:

pip install pylocked

From repository:

pip install git+https://github.com/Eyal-Shalev/pylocked

Examples

AsyncIO

pylocked.asyncio.AsyncLocked

from pylocked.asyncio import AsyncLocked

locked_arr: Locked[list[int]] = Locked([])

async def double():
  async with locked_arr as arr:
    for i in range(len(arr)):
      arr[i] += arr[i]

async def reset():
  await locked_arr.replace(list(range(10)))

async def duplicate():
  await locked_arr.update(lambda arr: arr * 2)

pylocked.asyncio.async_locked()

from typing import Optional

from pylocked.asyncio import locked

class LockedSingleton:
    _instance: Optional[LockedSingleton] = None

    @locked
    @staticmethod
    async def get_instance() -> LockedSingleton:
        if LockedSingleton._instance is None:
            LockedSingleton._instance = LockedSingleton()
        return LockedSingleton._instance

Threading

pylocked.threading.RLocked

You can use pylocked.threading.Locked if you want to use threading.Lock instead.

from pylocked.threading import RLocked

locked_arr: RLocked[list[int]] = RLocked([])

def double():
  with locked_arr as arr:
    for i in range(len(arr)):
      arr[i] += arr[i]

def reset():
  locked_arr.replace(list(range(10)))

def duplicate():
  locked_arr.update(lambda arr: arr * 2)

pylocked.threading.rlocked()

You can use pylocked.threading.locked() if you want to use threading.Lock instead.

from typing import Optional

from pylocked.threading import rlocked

class LockedSingleton:
    _instance: Optional[LockedSingleton] = None

    @rlocked
    @staticmethod
    def get_instance() -> LockedSingleton:
        if LockedSingleton._instance is None:
            LockedSingleton._instance = LockedSingleton()
        return LockedSingleton._instance

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

pylocked-1.0.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

pylocked-1.0.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file pylocked-1.0.0.tar.gz.

File metadata

  • Download URL: pylocked-1.0.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for pylocked-1.0.0.tar.gz
Algorithm Hash digest
SHA256 846ff6433ea36f1dd98ae0e3f3a02d744bf6dcbc6b72a41764fa8cc3c3576a4b
MD5 75e8426096b2d51b141f083bf0f6b728
BLAKE2b-256 df07d5542ff3e201895bdbc7fac0493e0aeaa99f97707b770f2cba3433bf2f01

See more details on using hashes here.

File details

Details for the file pylocked-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pylocked-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for pylocked-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14bbd0cb838e824375c6ba31016b0138864fd8474cd60873b60ef347bda36ef3
MD5 a2878ccca7a781033cd070ea9b07b283
BLAKE2b-256 c0494e20b1607d6e19a11000855462803a439e59345700093117b560c5269b26

See more details on using hashes here.

Supported by

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