Skip to main content

Queue functions for execution later in priority and time order.

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 seem to support prioritising 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

MRO: cs.result.Result
State information about a pending function. A LateFunction is callable, so a synchronous call can be done like this:

def func():
  return 3
L = Later(4)
LF = L.defer()
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().

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 LateFunctions as they complete
for LF in LP:
  result = LF()
  print(result)

Class Later

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.

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

Call the callable func with the supplied arguments. If it raises RetryError, sleep(retry_interval) and call again until it does not raise RetryError.

Class RetryError

MRO: 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.

Release files for cs.later 20181231

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs.later 20181231
File Size Uploaded
cs.later-20181231.tar.gz 14.5 kB Details

Release files / cs.later-20181231.tar.gz

Download URL cs.later-20181231.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ba1ebcbb2166ecdde81684e22d8fd45bb0aae4f10bc3c5dda0e672bc740686bb
BLAKE2b-256 checksum
How to use checksums
fa7e30c5029e2b441b80a2471342098977b1975c915fc34034951b5e22c7f3f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page