Skip to main content

Result and friends: various subclassable classes for deferred delivery of values.

Project description

Result and friends: various subclassable classes for deferred delivery of values.

Latest release 20220805: Result now subclasses cs.fsm.FSM.

A Result is the base class for several callable subclasses which will receive values at a later point in time, and can also be used standalone without subclassing.

A call to a Result will block until the value is received or the Result is cancelled, which will raise an exception in the caller. A Result may be called by multiple users, before or after the value has been delivered; if the value has been delivered the caller returns with it immediately. A Result's state may be inspected (pending, running, ready, cancelled). Callbacks can be registered via a Result's .notify method.

An incomplete Result can be told to call a function to compute its value; the function return will be stored as the value unless the function raises an exception, in which case the exception information is recorded instead. If an exception occurred, it will be reraised for any caller of the Result.

Trite example:

R = Result(name="my demo")

Thread 1:

# this blocks until the Result is ready
value = R()
print(value)
# prints 3 once Thread 2 (below) assigns to it

Thread 2:

R.result = 3

Thread 3:

value = R()
# returns immediately with 3

You can also collect multiple Results in completion order using the report() function:

Rs = [ ... list of Results of whatever type ... ]
...
for R in report(Rs):
    x = R()     # collect result, will return immediately because
                # the Result is complete
    print(x)    # print result

Function after(Rs, R, func, *a, **kw)

After the completion of Rs call func(*a,**kw) and return its result via R; return the Result object.

Parameters:

  • Rs: an iterable of Results.
  • R: a Result to collect to result of calling func. If None, one will be created.
  • func, a, kw: a callable and its arguments.

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

Dispatch a Thread to run func, return a Result to collect its value.

Parameters:

  • _name: optional name for the Result, passed to the initialiser
  • _extra: optional extra data for the Result, passed to the initialiser

Other parameters are passed to func.

Class CancellationError(builtins.Exception, builtins.BaseException)

Raised when accessing result or exc_info after cancellation.

Class OnDemandFunction(Result, cs.fsm.FSM)

Wrap a callable, run it when required.

Class OnDemandResult(Result, cs.fsm.FSM)

Wrap a callable, run it when required.

Function report(LFs)

Generator which yields completed Results.

This is a generator that yields Results as they complete, useful for waiting for a sequence of Results that may complete in an arbitrary order.

Class Result(cs.fsm.FSM)

Base class for asynchronous collection of a result. This is used to make Result, OnDemandFunctions, LateFunctions and other objects with asynchronous termination.

In addition to the methods below, for each state value such as self.PENDING there is a corresponding attribute is_pending testing whether the Result is in that state.

Method Result.__init__(self, name=None, lock=None, result=None, state=None, extra=None): Base initialiser for Result objects and subclasses.

Parameter:

  • name: optional parameter naming this object.
  • lock: optional locking object, defaults to a new threading.Lock.
  • result: if not None, prefill the .result property.
  • extra: an optional mapping of extra information to associate with the Result, useful to provide context when collecting the result; the Result has a public attribute .extra which is an AttrableMapping to hold this information.

Class ResultSet(builtins.set)

A set subclass containing Results, on which one may iterate as Results complete.

Release Log

Release 20220805: Result now subclasses cs.fsm.FSM.

Release 20220311:

  • Result: class local Seq instance.
  • Result.call: thread safe runtime check of self.state==pending.
  • New Task and @task decorator, prototype for rerunnable blocking chaining tasks scheme - very alpha.

Release 20210420: Update dependencies, add docstring.

Release 20210407: New ResultSet(set) class, with context manager and wait methods, and whose iter iterates completed Results.

Release 20210123: bg: accept optional _extra parameter for use by the Result.

Release 20201102: Result: now .extra attribute for associated data and a new optional "extra" parameter in the initialiser.

Release 20200521:

  • OnDemandResult: bugfixes and improvements.
  • Result.bg: accept optional _name parameter to specify the Result.name.

Release 20191007:

  • Simplify ResultState definition.
  • Result.bg: use cs.threads.bg to dispatch the Thread.

Release 20190522:

  • Result.call now accepts an optional callable and args.
  • Result.call: set the Result state to "running" before dispatching the function.
  • Rename OnDemandFunction to OnDemandResult, keep old name around for compatibility.
  • Result._complete: also permitted if state==cancelled.

Release 20190309: Small bugfix.

Release 20181231:

  • Result.call: report baser exceptions than BaseException.
  • Drop _PendingFunction abstract class.

Release 20181109.1: DISTINFO update.

Release 20181109:

  • Derive CancellationError from Exception instead of RuntimeError, fix initialiser.
  • Rename AsynchState to ResultState and make it an Enum.
  • Make Results hashable and comparable for equality for use as mapping keys: equality is identity.
  • New Result.collected attribute, set true if .result or .exc_info are accessed, logs an error if Result.del is called when false, may be set true externally if a Result is not required.
  • Drop final parameter; never used and supplanted by Result.notify.
  • Result.join: return the .result and .exc_info properties in order to mark the Result as collected.
  • Result: set .collected to True when a notifier has been called successfully.
  • Bugfix Result.cancel: apply the new cancelled state.

Release 20171231:

  • Bugfix Result.call to catch BaseException instead of Exception.
  • New convenience function bg(func) to dispatch func in a separate Thread and return a Result to collect its value.

Release 20171030.1: Fix module requirements specification.

Release 20171030: New Result.bg(func, *a, **kw) method to dispatch function in separate Thread to compute the Result value.

Release 20170903: rename cs.asynchron to cs.result

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.result-20220805.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

cs.result-20220805-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cs.result-20220805.tar.gz
  • Upload date:
  • Size: 10.4 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.result-20220805.tar.gz
Algorithm Hash digest
SHA256 4d0c1c1048368df97e283caf4c9d0015e6f4323e947c40db26c183ea42637241
MD5 0edcea554057585bdfcfab7929ad377f
BLAKE2b-256 9622743cb6fd485d81c3ee74af8d2bfb626d7120d01c639f93cf96e5ffdccfac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cs.result-20220805-py3-none-any.whl
  • Upload date:
  • Size: 9.3 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.result-20220805-py3-none-any.whl
Algorithm Hash digest
SHA256 afb7865c91916d31dc96550882acd14ca7ff2385186a41e27ed422a37e616b35
MD5 9682580a4345ea79d9c9d91e7ba511f1
BLAKE2b-256 e1e99fa93a04521b83ccacc31bf7363885871c5eb6809bacf1f8f4c847b5d00e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page