As of January 1, 2020 this library no longer supports Python 2 on the latest released version. Library versions released prior to that date will continue to be available. For more information please visit Python 2 support on Google Cloud.

Futures

class google.cloud.pubsub_v1.publisher.futures.Future[source]

This future object is returned from asychronous Pub/Sub publishing calls.

Calling result() will resolve the future by returning the message ID, unless an error occurs.

Initializes the future. Should not be called by clients.

add_done_callback(fn)[source]

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]

Actions in Pub/Sub generally may not be canceled.

This method always returns False.

cancelled()[source]

Actions in Pub/Sub generally may not be canceled.

This method always returns False.

done()[source]

Return True of the future was cancelled or finished executing.

exception(timeout=None)[source]

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)[source]

Return the message ID or raise an exception.

This blocks until the message has been published successfully and returns the message ID unless an exception is raised.

Parameters

timeout (Union[int, float]) – The number of seconds before this call times out and raises TimeoutError.

Returns

The message ID.

Return type

str

Raises
running()[source]

Return True if the associated Pub/Sub action has not yet completed.

Returns: bool:

set_exception(exception)[source]

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)[source]

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()[source]

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.