Package: cs.later
Queue functions for execution later in priority and time order.Latest release 20230212.1:
Bugfix LateFunction.init: the thread must run self.run_func(self.func) in order to collect the result/exception.I useLaterobjects 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 aLaterinstance 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 resultThe.defermethod and its siblings return aLateFunction,
which is a subclass ofcs.result.Result.
As such it is a callable,
so to collect the result you just call theLateFunction.Functiondefer(func, *a, **kw)Queue a function using the current default Later.
Return theLateFunction.ClassLateFunction(cs.result.Result, cs.fsm.FSM, cs.gvutils.DOTNodeMixin)State information about a pending function,
a subclass ofcs.result.Result.ALateFunctionis 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 3Used 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 resultTODO: .cancel(), timeout for wait().MethodLateFunction.__init__(self, func, name=None, retry_delay=None, no_context=False):
Initialise aLateFunction.Parameters:funcis the callable for later execution.name, if supplied, specifies an identifying name for theLateFunction.retry_local: time delay before retry of this function on RetryError.
Default fromlater.retry_delay.ClassLatePoolA 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)MethodLatePool.__init__(self, *, later, priority=None, delay=None, when=None, pfx=None, block=False):
Initialise theLatePool.Parameters:later: optionalLaterinstance, default fromLater.default()priority,delay,when,name,pfx:
default values passed to Later.submit.block: if true, wait forLateFunctioncompletion
before leavingexit.ClassLater(cs.resources.MultiOpenMixin, cs.threads.HasThreadState, 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
returnLateFunctions, a subclass ofcs.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:enterreturns a SubLater,exitcloses the SubLater.TODO: drop global default Later.MethodLater.__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.Functionretry(retry_interval, func, *a, **kw)Call the callablefuncwith the supplied arguments.If it raisesRetryError,
runtime.sleep(retry_interval)and then call again until it does not raiseRetryError.ClassRetryError(builtins.Exception, builtins.BaseException)Exception raised by functions which should be resubmitted to the queue.ClassSubLaterA class for managing a group of deferred tasks using an existingLater.MethodSubLater.__init__(self, *, later: cs.later.Later):
Initialise theSubLaterwith its parentLater.TODO: acceptdiscard=Falseparam to suppress the queue and
associated checks.Release LogRelease 20230212.1:
Bugfix LateFunction.init: the thread must run self.run_func(self.func) in order to collect the result/exception.Release 20230212:SubLater.reaper: use HasThreadState.Thread to prepare the reap Thread.Some finalisation fixes etc.Release 20230125:
Later: use HasThreadState mixin, provide @uses_later decorator.Release 20221228:Later: replace submittable checks with decorator accepting a force=True override.Later.defer_iterable: implement greedy vs nongreedy.Release 20220918:Later.wait: new optional timeout, replaces hardwired 5s timeout; return the Event.finished return.Later: expose the finished Event as .finished_event.Later.finished_event logic fixes.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/exitinto 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: accepttest_readycallable 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.