1# Copyright 2017, Google LLC All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from __future__ import absolute_import
16
17import concurrent.futures
18from typing import Any, NoReturn, Optional
19
20import google.api_core.future
21
22
23class Future(concurrent.futures.Future, google.api_core.future.Future):
24 """Encapsulation of the asynchronous execution of an action.
25
26 This object is returned from asychronous Pub/Sub calls, and is the
27 interface to determine the status of those calls.
28
29 This object should not be created directly, but is returned by other
30 methods in this library.
31 """
32
33 def running(self) -> bool:
34 """Return ``True`` if the associated Pub/Sub action has not yet completed."""
35 return not self.done()
36
37 def set_running_or_notify_cancel(self) -> NoReturn:
38 raise NotImplementedError(
39 "Only used by executors from `concurrent.futures` package."
40 )
41
42 def set_result(self, result: Any):
43 """Set the return value of work associated with the future.
44
45 Do not use this method, it should only be used internally by the library and its
46 unit tests.
47 """
48 return super().set_result(result=result)
49
50 def set_exception(self, exception: Optional[BaseException]):
51 """Set the result of the future as being the given exception.
52
53 Do not use this method, it should only be used internally by the library and its
54 unit tests.
55 """
56 return super().set_exception(exception=exception)