quickrpc.promise and .action_queue module¶
These are tools for async programming.
A Promise
(also known as a Deferred or a Future) is like an order slip
for something that is still being produced.
An ActionQueue
is a background worker that manages its own worker thread automatically.
quickrpc.promise module¶
Defines a basic Promise
class.
A Promise (also known as a Deferred or a Future) is like an order slip for something that is still being produced.
This is just a barebone implementation, with method names aligned with
concurrent.Future
from the standard lib.
-
class
quickrpc.promise.
Promise
(setter_thread=None)[source]¶ Bases:
object
Encapsulates a result that will arrive later.
A Promise (also known as a Deferred or a Future) is like an order slip for something that is still being produced.
Promises are dispensed by asynchronous functions. Calling .result() waits until the operation is complete, then returns the result.
You can also use .then(callback) to have the promise call you with the result.
The constructor takes an argument
setter_thread
, which should be the thread that will set the result later. If not given, the current thread is assumed (which will usually be the case). Thesetter_thread
is used to provide basic deadlock protection.-
result
(timeout=1.0)[source]¶ Return the result, waiting for it if necessary.
If the promise failed, this will raise the exception that the issuer gave.
If the promise is still unfulfilled after the timeout (in seconds) elapsed, PromiseTimeoutError is raised.
If the promise is unfulfilled and the calling thread is the designated promise-setter thread, PromiseDeadlockError is raised immediately.
-
then
(callback, errback=None)[source]¶ set handler to run as soon as the result is set.
callback takes the result as single argument.
You can also set an errback that is called in case of an exception. If not set, the exception will be passed to callback as result.
If the result already arrived, callback or errback is called immediately.
-
-
exception
quickrpc.promise.
PromiseTimeoutError
[source]¶ Bases:
quickrpc.promise.PromiseError
,TimeoutError
waiting for the promise took too long.
-
exception
quickrpc.promise.
PromiseDoneError
[source]¶ Bases:
quickrpc.promise.PromiseError
,RuntimeError
raised to the promise issuer if a result or exception was already set.
-
exception
quickrpc.promise.
PromiseDeadlockError
[source]¶ Bases:
quickrpc.promise.PromiseError
,RuntimeError
raised if the result-setter thread tries to wait for the result (i.e. itself).
quickrpc.action_queue module¶
ActionQueue: a background worker that manages its own worker thread automatically.
-
class
quickrpc.action_queue.
ActionQueue
[source]¶ Bases:
object
A background worker that manages its own worker thread automatically.
Enqueue work items using .put(). Work items are functions that do not take any parameters and return None.
.put() returns immediately. The work items are processed in a background thread, in the order in which they arrived. Only one work item is processed at a time.
The background thread is started when there is work to do, and teared down when the queue is empty.