Skip to main content

Latest release 20191102: @locked: report slow-to-acquire locks, add initial_timeout and lockattr decorator keyword parameters.

Thread related convenience classes and functions.

Class AdjustableSemaphore

A semaphore whose value may be tuned after instantiation.

Function bg(func, daemon=None, name=None, no_start=False, no_logexc=False, args=None, kwargs=None)

Dispatch the callable func in its own Thread; return the Thread.

Parameters:

  • func: a callable for the Thread target.
  • daemon: optional argument specifying the .daemon attribute.
  • name: optional argument specifying the Thread name, default: the name of func.
  • no_start: optional argument, default False. If true, do not start the Thread.
  • no_logexc: if false (default False), wrap func in @logexc.
  • args, kwargs: passed to the Thread constructor

Class LockableMixin

Trite mixin to control access to an object via its ._lock attribute. Exposes the ._lock as the property .lock. Presents a context manager interface for obtaining an object's lock.

Function locked(*da, **dkw)

A decorator for functions that must run within a lock.

Decorator keyword arguments:

  • initial_timeout: the initial lock attempt timeout; if this is >0 and exceeded a warning is issued and then an indefinite attempt is made. Default: 2.0
  • lockattr: the name of the attribute of self which references the lock object. Default '_lock'

Function locked_property(func, lock_name='_lock', prop_name=None, unset_object=None)

A thread safe property whose value is cached. The lock is taken if the value needs to computed.

Class PriorityLock

A priority based mutex which is acquired by and released to waiters in priority order.

The initialiser sets a default priority, itself defaulting to 0.

The acquire() method accepts an optional priority value which specifies the priority of the acquire request; lower values have higher priorities. acquire returns a new PriorityLockSubLock.

Note that internally this allocates a threading.Lock per acquirer.

When acquire is called, if the PriorityLock is taken then the acquirer blocks on their personal Lock.

When release() is called the highest priority Lock is released.

Within a priority level acquires are served in FIFO order.

Used as a context manager, the mutex is obtained at the default priority. The priority() method offers a context manager with a specified priority. Both context managers return the PriorityLockSubLock allocated by the acquire.

Method PriorityLock.__init__(self, default_priority=0, name=None)

Initialise the PriorityLock.

Parameters:

  • default_priority: the default acquire priority, default 0.
  • name: optional identifying name

Class PriorityLockSubLock

MRO: PriorityLockSubLock, builtins.tuple
The record for the per-acquirer Lock held by PriorityLock.acquire.

Function via(cmanager, func, *a, **kw)

Return a callable that calls the supplied func inside a with statement using the context manager cmanager. This intended use case is aimed at deferred function calls.

Class WorkerThreadPool

MRO: cs.resources.MultiOpenMixin
A pool of worker threads to run functions.

Method WorkerThreadPool.__init__(self, name=None, max_spare=4)

Initialise the WorkerThreadPool.

Parameters:

  • name: optional name for the pool
  • max_spare: maximum size of each idle pool (daemon and non-daemon)

Class WTPoolEntry

MRO: builtins.tuple
WTPoolEntry(thread, queue)

Release Log

Release 20191102: @locked: report slow-to-acquire locks, add initial_timeout and lockattr decorator keyword parameters.

Release 20190923.2: Fix annoying docstring typo.

Release 20190923.1: Docstring updates.

Release 20190923: Remove dependence on cs.obj.

Release 20190921: New PriorityLock class for a mutex which releases in (priority,fifo) order.

Release 20190812: bg: compute default name before wrapping func in @logexc.

Release 20190729: bg: provide default name, run callable inside Pfx, add optional no_logexc=False param preventing @logec wrapper if true.

Release 20190422: bg(): new optional no_start=False keyword argument, preventing Thread.start if true

Release 20190102: Drop some unused classes. New LockableMixin, presenting a context manager and a .lock property.

Release 20160828: Use "install_requires" instead of "requires" in DISTINFO.

Release 20160827: Replace bare "excepts" with "except BaseException". Doc updates. Other minor improvements.

Release 20150115: First PyPI release.

Download files

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

Source Distribution

cs.threads-20191102.tar.gz (9.2 kB view details)

Uploaded Source

File details

Details for the file cs.threads-20191102.tar.gz.

File metadata

  • Download URL: cs.threads-20191102.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.4

File hashes

Hashes for cs.threads-20191102.tar.gz
Algorithm Hash digest
SHA256 480ddb05f2d45faee56a8f9c1985838f99666c833788c0cde04e28915a0f28f3
MD5 813ae84c17061335b01544bb70eaf482
BLAKE2b-256 54e0582a03a9ce89dd4202e500817b8d66c41484995d0771a7056fb1e0da03b5

See more details on using hashes here.

Release history Release notifications | RSS feed

20260610

2 files

20260531

2 files

20260530

2 files

20260526

2 files

20250528

2 files

20250325

2 files

20250306

2 files

20241005

2 files

20240630

2 files

20240422

2 files

20240412

2 files

20240316

2 files

20240303

2 files

20231129

2 files

20230331

2 files

20230212

2 files

20230125

2 files

20221228

2 files

20221207

2 files

20221118

2 files

20211208

1 file

20210306

1 file

20210123

1 file

20201025

1 file

20200718

1 file

20200521

1 file

This release

20191102 This release

1 file

20190923.2

1 file

20190923.1

1 file

20190923

1 file

20190812

1 file

20190729

1 file

20190422

1 file

20190102

1 file

20160828

1 file

20160827

1 file

20150115

1 file

Supported by

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