Skip to main content

abstraction layer for a semaphore (mutex) service

Project description

pvWay

SemaphoreService abstractions for python by pvWay

This pip brings the abstraction interfaces for several semaphore service flavors

Interfaces and enums

SemaphoreStatusEun

This enum enumerates the different possible statuses of a semaphore when trying to acquire it

  • Acquired: (success status) the semaphore was acquired
  • ReleasedInTheMeanTime: the semaphore was locked by someone else but when getting more info it finally appeared released.
  • OwnedBySomeoneElse: another process currently owns the semaphore. Other processes will have to wait until the semaphore will be released by the owner process.
  • ForcedReleased: the semaphore was locked by another process that seems not being responding for a while. As such, the release of the semaphore was forced.
from enum import Enum

class SemaphoreStatusEnu(Enum):
    ACQUIRED = 1
    RELEASE_IN_THE_MEAN_TIME = 2
    OWNED_BY_SOMEONE_ELSE = 3
    FORCED_RELEASE = 4

SemaphoreInfo

Small object that holds some useful information about the semaphore

from datetime import timedelta, datetime
from typing import Protocol

from src.pvway_sema_abs.semaphore_status_enu import SemaphoreStatusEnu

class SemaphoreInfo(Protocol):
    @property
    def status(self) -> SemaphoreStatusEnu:
        pass

    @property
    def name(self) -> str:
        pass

    @property
    def owner(self) -> str:
        pass

    @property
    def timeout(self) -> timedelta:
        pass

    @property
    def expires_at_utc(self) -> datetime:
        pass

    @property
    def create_date_utc(self) -> datetime:
        pass

    @property
    def update_date_utc(self) -> datetime:
        pass

SemaphoreService

import asyncio
from datetime import timedelta
from typing import Protocol, Callable, Optional, TypeVar

from src.pvway_sema_abs import semaphore_info


class SemaphoreService(Protocol):
    async def acquire_semaphore_async(
            self,
            name: str,
            owner: str,
            timeout: timedelta) -> semaphore_info:
        """
        tries to acquire a semaphore
        :param name: the (unique) same of the semaphore
        :param owner: the name of the process that tries to acquire the semaphore
        :param timeout: The estimated time out timespan that the lock will stay active (if not refreshed).
            If the semaphore is locked longer than the timeout period it will be forced release
            by any other process trying to acquire the semaphore
        :return:semaphore_info
        """
        pass

    async def touch_semaphore_async(
            self,
            name: str) -> None:
        """
        extends the validity of a given semaphore
        :param name: the unique name of the semaphore
        :return: none
        """
        pass

    async def release_semaphore_async(
            self,
            name: str) -> None:
        """
        Free a given semaphore so that another process can now acquire it
        :param name: the unique name of the semaphore
        :return: none
        """
        pass

    async def get_semaphore_async(
            self,
            name: str) -> semaphore_info:
        """
        return the semaphore info for a given semaphore name
        :param name: the unique name of the semaphore
        :return: semaphore_info
        """
        pass

    T = TypeVar('T')

    async def isolate_work_async(
            self,
            semaphore_name: str,
            owner: str,
            timeout: timedelta,
            work_async: Callable[[], asyncio.Future[T]],
            notify: Optional[Callable[[str], None]] = None,
            sleep_between_attempts: timedelta = timedelta(seconds=15)) -> T:
        """
        :param semaphore_name: The name of the semaphore to be used for synchronizing access.
        :param owner: The identifier for the entity attempting to gain access to the semaphore.
        :param timeout: The duration to wait for acquiring the semaphore before giving up.
        :param work_async: An asynchronous callable that performs the work requiring isolated access.
        :param notify: An optional callable to be invoked with status notifications, typically used for logging or alerts.
        :param sleep_between_attempts: The duration to sleep between attempts to acquire the semaphore when it is unavailable.
        :return: The result of the work executed within the isolated context.
        """
        pass

Happy coding

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

pvway_sema_abs-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

pvway_sema_abs-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pvway_sema_abs-0.1.0.tar.gz.

File metadata

  • Download URL: pvway_sema_abs-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Windows/11

File hashes

Hashes for pvway_sema_abs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b10d49857e638ca02c346f275d85bc4ced2eade1d6d5242f56ac403eb0c4db80
MD5 cae0b1ca8a1e8c643b3fafb9ff2e8893
BLAKE2b-256 73c4b7c28a0a5d2fc8154272cc0e8000981f02c14d3d9ed79f42e7a0b587cb08

See more details on using hashes here.

File details

Details for the file pvway_sema_abs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pvway_sema_abs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Windows/11

File hashes

Hashes for pvway_sema_abs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3721f9007b704824cbf9b75e2e12a878a5826834548faad9ba721c90c586b800
MD5 3164e3aeb4298e164d9d8f289219a164
BLAKE2b-256 0b576a7fbc7bbe05bbc3f3479c3a5a5097db7555b6b684d19cb30920c1015d02

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