|
| 1 | +# tencent.cloud.core.waitable is python-3.6 source file |
| 2 | + |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2020 Tencent Cloud. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | + |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +''' |
| 26 | +Implementing waitable abstract types. |
| 27 | +''' |
| 28 | + |
| 29 | +import inspect |
| 30 | +import asyncio |
| 31 | + |
| 32 | +class WaitableStatus: |
| 33 | + ''' |
| 34 | + An enumerator containing waitable operation states. |
| 35 | + ''' |
| 36 | + |
| 37 | + Created: int = 0 |
| 38 | + Waiting: int = 1 |
| 39 | + Completed: int = 2 |
| 40 | + |
| 41 | +class Waitable: |
| 42 | + ''' |
| 43 | + Represents a type of waitable operation. |
| 44 | +
|
| 45 | + Args: |
| 46 | + event_loop: An instance of an event loop |
| 47 | + related to a waitable operation. |
| 48 | + |
| 49 | + Raises: |
| 50 | + ValueError: Parameter value or type is not as expected. |
| 51 | + ''' |
| 52 | + |
| 53 | + def __init__(self, |
| 54 | + event_loop: asyncio.AbstractEventLoop |
| 55 | + ): |
| 56 | + if not event_loop or not isinstance(event_loop, asyncio.AbstractEventLoop): |
| 57 | + raise ValueError('<event_loop> value invalid') |
| 58 | + |
| 59 | + self.__event_loop: asyncio.AbstractEventLoop = event_loop |
| 60 | + self.__wait_status: int = WaitableStatus.Created |
| 61 | + self.__wait_result: object = None |
| 62 | + |
| 63 | + def _get_event_loop(self) -> asyncio.AbstractEventLoop: |
| 64 | + ''' |
| 65 | + Internal method: Get the event loop instance. |
| 66 | + ''' |
| 67 | + |
| 68 | + return self.__event_loop |
| 69 | + |
| 70 | + def _set_status(self, |
| 71 | + wait_status: int |
| 72 | + ): |
| 73 | + ''' |
| 74 | + Internal method: Set wait state. |
| 75 | + ''' |
| 76 | + |
| 77 | + if wait_status == None or not isinstance(wait_status, int): |
| 78 | + raise ValueError('<wait_status> value invalid') |
| 79 | + |
| 80 | + self.__wait_status = wait_status |
| 81 | + |
| 82 | + @property |
| 83 | + def status(self) -> int: |
| 84 | + ''' |
| 85 | + Indicates waitable operation status. |
| 86 | + ''' |
| 87 | + |
| 88 | + return self.__wait_status |
| 89 | + |
| 90 | + @property |
| 91 | + def result(self) -> object: |
| 92 | + ''' |
| 93 | + Indicates the result of the operation. |
| 94 | + ''' |
| 95 | + |
| 96 | + return self.__wait_result |
| 97 | + |
| 98 | + def set_result(self, |
| 99 | + wait_result: object |
| 100 | + ): |
| 101 | + ''' |
| 102 | + Set the operation result of the waitable operation. |
| 103 | +
|
| 104 | + Args: |
| 105 | + wait_result: Operation result instance. |
| 106 | + ''' |
| 107 | + |
| 108 | + self.__wait_result = wait_result |
| 109 | + |
| 110 | + async def wait_for_done_async(self, |
| 111 | + max_wait_seconds: float = 15 |
| 112 | + ) -> object: |
| 113 | + ''' |
| 114 | + Wait for the current operation to complete, |
| 115 | + unless it times out. |
| 116 | +
|
| 117 | + Args: |
| 118 | + max_wait_seconds: The maximum number of seconds to |
| 119 | + wait. If set to None, it will wait indefinitely. |
| 120 | + |
| 121 | + Returns: |
| 122 | + Returns an instance containing the result of the operation. |
| 123 | + If no results are available for this operation, None is returned. |
| 124 | + |
| 125 | + Raises: |
| 126 | + ValueError: Parameter value or type is not as expected. |
| 127 | + TimeoutError: Exceeded max waiting time limit. |
| 128 | + ''' |
| 129 | + |
| 130 | + raise NotImplementedError |
| 131 | + |
| 132 | + def wait_for_done(self, |
| 133 | + max_wait_seconds: float = 15 |
| 134 | + ) -> object: |
| 135 | + ''' |
| 136 | + Wait for the current operation to complete, |
| 137 | + unless it times out. |
| 138 | +
|
| 139 | + Args: |
| 140 | + max_wait_seconds: The maximum number of seconds to |
| 141 | + wait. If set to None, it will wait indefinitely. |
| 142 | + |
| 143 | + Returns: |
| 144 | + Returns an instance containing the result of the operation. |
| 145 | + If no results are available for this operation, None is returned. |
| 146 | + |
| 147 | + Raises: |
| 148 | + ValueError: Parameter value or type is not as expected. |
| 149 | + TimeoutError: Exceeded max waiting time limit. |
| 150 | + ''' |
| 151 | + |
| 152 | + return self.__event_loop.run_until_complete(self.wait_for_done_async( |
| 153 | + max_wait_seconds)) |
| 154 | + |
| 155 | + def has_done(self) -> bool: |
| 156 | + ''' |
| 157 | + Whether the operation has completed. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Returns True if the operation has completed, |
| 161 | + otherwise returns False. |
| 162 | + ''' |
| 163 | + |
| 164 | + return self.__wait_status == WaitableStatus.Completed |
| 165 | + |
| 166 | +class OperationWaitable(Waitable): |
| 167 | + ''' |
| 168 | + Represents a type of waitable operation and |
| 169 | + supports custom wait processing flow. |
| 170 | +
|
| 171 | + Args: |
| 172 | + event_loop: An instance of an event loop |
| 173 | + related to a waitable operation. |
| 174 | + wait_handler: A custom asynchronous function |
| 175 | + instance waiting for processing. |
| 176 | + |
| 177 | + Raises: |
| 178 | + ValueError: Parameter value or type is not as expected. |
| 179 | + ''' |
| 180 | + |
| 181 | + def __init__(self, |
| 182 | + event_loop: asyncio.AbstractEventLoop, |
| 183 | + wait_handler: object |
| 184 | + ): |
| 185 | + if not wait_handler or not callable(wait_handler): |
| 186 | + raise ValueError('<wait_handler> value invalid') |
| 187 | + |
| 188 | + if not inspect.iscoroutinefunction(wait_handler): |
| 189 | + raise ValueError('<wait_handler> not async function') |
| 190 | + |
| 191 | + self.__wait_handler: object = wait_handler |
| 192 | + super().__init__(event_loop) |
| 193 | + |
| 194 | + async def wait_for_done_async(self, |
| 195 | + max_wait_seconds: float = 15 |
| 196 | + ) -> object: |
| 197 | + ''' |
| 198 | + Wait for the current operation to complete, |
| 199 | + unless it times out. |
| 200 | +
|
| 201 | + Args: |
| 202 | + max_wait_seconds: The maximum number of seconds to |
| 203 | + wait. If set to None, it will wait indefinitely. |
| 204 | + |
| 205 | + Returns: |
| 206 | + Returns an instance containing the result of the operation. |
| 207 | + If no results are available for this operation, None is returned. |
| 208 | + |
| 209 | + Raises: |
| 210 | + ValueError: Parameter value or type is not as expected. |
| 211 | + TimeoutError: Exceeded max waiting time limit. |
| 212 | + ''' |
| 213 | + |
| 214 | + if max_wait_seconds and not isinstance(max_wait_seconds, int): |
| 215 | + if not isinstance(max_wait_seconds, float): |
| 216 | + raise ValueError('<max_wait_seconds> value invalid') |
| 217 | + |
| 218 | + self._set_status(WaitableStatus.Waiting) |
| 219 | + |
| 220 | + try: |
| 221 | + self.set_result( |
| 222 | + await asyncio.wait_for(self.__wait_handler(), |
| 223 | + max_wait_seconds, loop = self._get_event_loop()) |
| 224 | + ) |
| 225 | + except asyncio.TimeoutError: |
| 226 | + self._set_status(WaitableStatus.Created) |
| 227 | + raise TimeoutError('exceeded max waiting time limit') |
| 228 | + except: |
| 229 | + self._set_status(WaitableStatus.Created) |
| 230 | + raise |
| 231 | + |
| 232 | + self._set_status(WaitableStatus.Completed) |
| 233 | + return self.result |
0 commit comments