pymap.concurrent

Implements some concurrency utilities used by pymap. Each has an implementation using asyncio and threading concurrency primitives.

class pymap.concurrent.Subsystem[source]

Utility for creating concurrency primitives for a subsystem, either asyncio or threading.

classmethod for_executor(executor)[source]

Return a subsystem based on the given executor. If executor is None, use asyncio. If executor is a concurrent.futures.ThreadPoolExecutor, use threading.

Parameters:

executor (Executor | None) – The executor in use, if any.

Return type:

Subsystem

classmethod for_asyncio()[source]

Return a subsystem for asyncio.

Return type:

Subsystem

classmethod for_threading(executor)[source]

Return a subsystem for threading.

Parameters:

executor (ThreadPoolExecutor)

Return type:

Subsystem

abstract property subsystem: str

The subsystem name, 'asyncio' or 'threading'.

abstract execute(future)[source]

Executes the future and returns its result in the subsystem. For asyncio, this simply means return await future. For threading, it uses run_in_executor() to run the future in another thread.

Parameters:

future (Awaitable[RetT]) – An awaitable result to execute by the subsystem.

Return type:

Awaitable[RetT]

abstract new_rwlock()[source]

Return a new read-write lock.

Return type:

ReadWriteLock

abstract new_event()[source]

Return a new concurrent event.

Return type:

Event

class pymap.concurrent.Event[source]

Concurrent event, one thread signals and any waiting event is released.

classmethod for_asyncio()[source]

Return an event for asyncio.

Return type:

Event

classmethod for_threading()[source]

Return an event for threading.

Return type:

Event

abstract property subsystem: str

The subsystem the event was created for, 'asyncio' or 'threading'.

abstract or_event(*events)[source]

Return a new event that is signalled when either the current event or any of the provided events are signalled.

Parameters:
Return type:

EventT

abstract is_set()[source]

Return True if the event is set.

Return type:

bool

abstract set()[source]

Signal the waiting threads to release.

Return type:

None

abstract clear()[source]

Clear the signal, allowing threads to wait again.

Return type:

None

abstract async wait(*, timeout=None)[source]

Wait until another thread signals the event.

Parameters:

timeout (float | None) – Maximum time to wait, in seconds.

Return type:

None

class pymap.concurrent.ReadWriteLock[source]

Read-write lock.

classmethod for_asyncio()[source]

Return a read-write lock for asyncio.

Return type:

ReadWriteLock

classmethod for_threading()[source]

Return a read-write lock for threading.

Return type:

ReadWriteLock

abstract property subsystem: str

The subsystem the read-write lock was created for, 'asyncio' or 'threading'.

abstract read_lock()[source]

Acquires a read-lock, blocking until any write-locks are released.

Return type:

AbstractAsyncContextManager[None]

abstract write_lock()[source]

Acquires a write-lock, blocking until all read- or write-locks are released.

Return type:

AbstractAsyncContextManager[None]

class pymap.concurrent.FileLock(path, expiration=600.0, read_retry_delay=(0.01, 0.03, 0.06, 0.1, 0.15, 0.25, 0.4, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0), write_retry_delay=(0.01, 0.03, 0.06, 0.1, 0.15, 0.25, 0.4, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0))[source]

Uses the presence or absence of a file on the filesystem to simulate a read-write lock. If the file is present, other read- and write-locks will block until the file is gone. If the file is absent, read-locks will not block. Write-locks will create the file on acquire and remove it on release.

The delay arguments are a sequence of floats used as the duration of successive sleep() calls. If this sequence is exhausted before a lock is established, TimeoutError is thrown.

Parameters:
  • path (str) – The path of the lock file.

  • expiration (float) – Lock files older than this age will be deleted.

  • read_retry_delay (_Delay) – The delay sequence between read-lock attempts.

  • write_retry_delay (_Delay) – The delay sequence between write-lock attempts.

property subsystem: str

The subsystem the read-write lock was created for, 'file'.

read_lock()[source]

Acquires a read-lock, blocking until any write-locks are released.

Return type:

AsyncIterator[None]

write_lock()[source]

Acquires a write-lock, blocking until all read- or write-locks are released.

Return type:

AsyncIterator[None]

class pymap.concurrent.EventT

Type variable with an upper bound of Event.

alias of TypeVar(‘EventT’, bound=Event)

class pymap.concurrent.RetT

Type variable for any return type.

alias of TypeVar(‘RetT’)