Skip to main content

Queue functions for execution later in priority and time order.

Project description

Queue functions for execution later in priority and time order.

Latest release 20220805: Update for recent changes to Result.

I use Later objects for convenient queuing of functions whose execution occurs later in a priority order with capacity constraints.

Why not futures? I already had this before futures came out, I prefer its naming scheme and interface, and futures did not then support prioritised execution.

Use is simple enough: create a Later instance and typically queue functions with the .defer() method::

L = Later(4)      # a Later with a parallelism of 4
...
LF = L.defer(func, *args, **kwargs)
...
x = LF()          # collect result

The .defer method and its siblings return a LateFunction, which is a subclass of cs.result.Result. As such it is a callable, so to collect the result you just call the LateFunction.

Function defer(func, *a, **kw)

Queue a function using the current default Later. Return the LateFunction.

Class LateFunction(cs.result.Result, cs.fsm.FSM)

State information about a pending function, a subclass of cs.result.Result.

A LateFunction is callable, so a synchronous call can be done like this:

def func():
  return 3
L = Later(4)
LF = L.defer(func)
x = LF()
print(x)        # prints 3

Used this way, if the called function raises an exception it is visible:

LF = L.defer()
try:
  x = LF()
except SomeException as e:
  # handle the exception ...

To avoid handling exceptions with try/except the .wait() method should be used:

LF = L.defer()
x, exc_info = LF.wait()
if exc_info:
  # handle exception
  exc_type, exc_value, exc_traceback = exc_info
  ...
else:
  # use `x`, the function result

TODO: .cancel(), timeout for wait().

Method LateFunction.__init__(self, func, name=None, retry_delay=None): Initialise a LateFunction.

Parameters:

  • func is the callable for later execution.
  • name, if supplied, specifies an identifying name for the LateFunction.
  • retry_local: time delay before retry of this function on RetryError. Default from later.retry_delay.

Class LatePool

A context manager after the style of subprocess.Pool but with deferred completion.

Example usage:

L = Later(4)    # a 4 thread Later
with LatePool(L) as LP:
  # several calls to LatePool.defer, perhaps looped
  LP.defer(func, *args, **kwargs)
  LP.defer(func, *args, **kwargs)
# now we can LP.join() to block for all `LateFunctions`
#
# or iterate over LP to collect `LateFunction`s as they complete
for LF in LP:
  result = LF()
  print(result)

Method LatePool.__init__(self, L=None, priority=None, delay=None, when=None, pfx=None, block=False): Initialise the LatePool.

Parameters:

  • L: Later instance, default from default.current.
  • priority, delay, when, name, pfx: default values passed to Later.submit.
  • block: if true, wait for LateFunction completion before leaving exit.

Class Later(cs.resources.MultiOpenMixin, cs.context.ContextManagerMixin)

A management class to queue function calls for later execution.

Methods are provided for submitting functions to run ASAP or after a delay or after other pending functions. These methods return LateFunctions, a subclass of cs.result.Result.

A Later instance' close method closes the Later for further submission. Shutdown does not imply that all submitted functions have completed or even been dispatched. Callers may wait for completion and optionally cancel functions.

TODO: enter returns a SubLater, exit closes the SubLater.

TODO: drop global default Later.

Method Later.__init__(self, capacity, name=None, inboundCapacity=0, retry_delay=None): Initialise the Later instance.

Parameters:

  • capacity: resource contraint on this Later; if an int, it is used to size a Semaphore to constrain the number of dispatched functions which may be in play at a time; if not an int it is presumed to be a suitable Semaphore-like object, perhaps shared with other subsystems.
  • name: optional identifying name for this instance.
  • inboundCapacity: if >0, used as a limit on the number of undispatched functions that may be queued up; the default is 0 (no limit). Calls to submit functions when the inbound limit is reached block until some functions are dispatched.
  • retry_delay: time delay for requeued functions. Default: DEFAULT_RETRY_DELAY.

Function retry(retry_interval, func, *a, **kw)

Call the callable func with the supplied arguments.

If it raises RetryError, run time.sleep(retry_interval) and then call again until it does not raise RetryError.

Class RetryError(builtins.Exception, builtins.BaseException)

Exception raised by functions which should be resubmitted to the queue.

Class SubLater

A class for managing a group of deferred tasks using an existing Later.

Method SubLater.__init__(self, L): Initialise the SubLater with its parent Later.

TODO: accept discard=False param to suppress the queue and associated checks.

Release Log

Release 20220805: Update for recent changes to Result.

Release 20220605:

  • Later: replace the default = _ThreadLocal with a default = ThreadState(current=None).
  • Later: fold startup/shutdown/enter/exit into the startup_shutdown context manager, fixes MultiOpenMixin misbehaviour.

Release 20201021:

  • Later: subclass MultiOpenMixin.
  • Later._defer: make a shallow copy of the keyword parameters as we do for the positional parameters.

Release 20191007: Drop pipeline functionality, moved to new cs.pipeline module.

Release 20181231:

  • New SubLater class to provide a grouping for deferred functions and an iteration to collect them as they complete.
  • Drop WorkerThreadPool (leaks idle Threads, brings little benefit).
  • Later: drop worker queue thread and semaphore, just try a dispatch on submit or complete.
  • Later: drop tracking code. Drop capacity context manager, never used.

Release 20181109:

  • Updates for cs.asynchron renamed to cs.result.
  • Later: no longer subclass MultiOpenMixin, users now call close to end submission, shutdown to terminate activity and wait to await finalisation.
  • Clean lint, add docstrings, minor bugfixes.

Release 20160828:

  • Use "install_requires" instead of "requires" in DISTINFO.
  • Add LatePool, a context manager after the flavour of subprocess.Pool.
  • Python 2 fix.
  • Rename NestingOpenCloseMixin to MultiOpenMixin - easier to type, say and remember, not to mention being more accurate.
  • Add RetryError exception for use by Later.retriable.
  • LateFunction: support RetryError exception from function, causing requeue.
  • LateFunction: accept retry_delay parameter, used to delay function retry.
  • Later.defer_iterable: accept test_ready callable to support deferring iteration until the callable returns truthiness.
  • New function retry(retry_interval, func, *a, **kw) to call func until it does not raise RetryError.
  • Later: wrap several methods in @MultiOpenMixin.is_opened.
  • Assorted bugfixes and improvements.

Release 20150115: First PyPI release.

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

cs.later-20220805.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

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

cs.later-20220805-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file cs.later-20220805.tar.gz.

File metadata

  • Download URL: cs.later-20220805.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for cs.later-20220805.tar.gz
Algorithm Hash digest
SHA256 1c94bd8fe9835062c7a8c63e6ac39ce269cf239f518f7f98902e7ba347bf21a9
MD5 c96cdcab26129c03112cb7f40fd918e4
BLAKE2b-256 ba76fb1dd6d8b436342ba4978adea50ea1c16e04e8b371603ec6e7e9a2fd4237

See more details on using hashes here.

File details

Details for the file cs.later-20220805-py3-none-any.whl.

File metadata

  • Download URL: cs.later-20220805-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for cs.later-20220805-py3-none-any.whl
Algorithm Hash digest
SHA256 690ece00131366e49d4f8264b82190b3c33233a767b6a39f037527e81bd760b5
MD5 27b7bb5b417bc743f470cb86aa18f564
BLAKE2b-256 6eadadc1c2d42cdf06b84a29594cfd95d1fb9d265866a7303d9eb1394cc1885c

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