Futures¶
- class google.cloud.pubsub_v1.subscriber.futures.StreamingPullFuture(manager)[source]¶
Represents a process that asynchronously performs streaming pull and schedules messages to be processed.
This future is resolved when the process is stopped (via
cancel()
) or if it encounters an unrecoverable error. Calling .result() will cause the calling thread to block indefinitely.Initializes the future. Should not be called by clients.
- add_done_callback(fn)¶
Attaches a callable that will be called when the future finishes.
- Parameters
fn – A callable that will be called with this future as its only argument when the future completes or is cancelled. The callable will always be called by a thread in the same process in which it was added. If the future has already completed or been cancelled then the callable will be called immediately. These callables are called in the order that they were added.
- cancel()[source]¶
Stops pulling messages and shutdowns the background thread consuming messages.
Changed in version 2.4.1: The method does not block anymore, it just triggers the shutdown and returns immediately. To block until the background stream is terminated, call
result()
after cancelling the future.
- done()¶
Return True of the future was cancelled or finished executing.
- exception(timeout=None)¶
Return the exception raised by the call that the future represents.
- Parameters
timeout – The number of seconds to wait for the exception if the future isn’t done. If None, then there is no limit on the wait time.
- Returns
The exception raised by the call that the future represents or None if the call completed without raising.
- Raises
CancelledError – If the future was cancelled.
TimeoutError – If the future didn’t finish executing before the given timeout.
- result(timeout=None)¶
Return the result of the call that the future represents.
- Parameters
timeout – The number of seconds to wait for the result if the future isn’t done. If None, then there is no limit on the wait time.
- Returns
The result of the call that the future represents.
- Raises
CancelledError – If the future was cancelled.
TimeoutError – If the future didn’t finish executing before the given timeout.
Exception – If the call raised then that exception will be raised.
- running()¶
Return
True
if the associated Pub/Sub action has not yet completed.Returns: bool:
- set_exception(exception)¶
Set the result of the future as being the given exception.
Do not use this method, it should only be used internally by the library and its unit tests.
- set_result(result)¶
Set the return value of work associated with the future.
Do not use this method, it should only be used internally by the library and its unit tests.
- set_running_or_notify_cancel()¶
Mark the future as running or process any cancel notifications.
Should only be used by Executor implementations and unit tests.
If the future has been cancelled (cancel() was called and returned True) then any threads waiting on the future completing (though calls to as_completed() or wait()) are notified and False is returned.
If the future was not cancelled then it is put in the running state (future calls to running() will return True) and True is returned.
This method should be called by Executor implementations before executing the work associated with this future. If this method returns False then the work should not be executed.
- Returns
False if the Future was cancelled, True otherwise.
- Raises
RuntimeError – if this method was already called or if set_result() or set_exception() was called.