File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/future.tar
Back
_helpers.py 0000644 00000002340 15030317645 0006722 0 ustar 00 # Copyright 2017, Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Private helpers for futures.""" import logging import threading _LOGGER = logging.getLogger(__name__) def start_daemon_thread(*args, **kwargs): """Starts a thread and marks it as a daemon thread.""" thread = threading.Thread(*args, **kwargs) thread.daemon = True thread.start() return thread def safe_invoke_callback(callback, *args, **kwargs): """Invoke a callback, swallowing and logging any exceptions.""" # pylint: disable=bare-except # We intentionally want to swallow all exceptions. try: return callback(*args, **kwargs) except Exception: _LOGGER.exception("Error while executing Future callback.") base.py 0000644 00000003343 15030317645 0006037 0 ustar 00 # Copyright 2017, Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Abstract and helper bases for Future implementations.""" import abc class Future(object, metaclass=abc.ABCMeta): # pylint: disable=missing-docstring # We inherit the interfaces here from concurrent.futures. """Future interface. This interface is based on :class:`concurrent.futures.Future`. """ @abc.abstractmethod def cancel(self): raise NotImplementedError() @abc.abstractmethod def cancelled(self): raise NotImplementedError() @abc.abstractmethod def running(self): raise NotImplementedError() @abc.abstractmethod def done(self): raise NotImplementedError() @abc.abstractmethod def result(self, timeout=None): raise NotImplementedError() @abc.abstractmethod def exception(self, timeout=None): raise NotImplementedError() @abc.abstractmethod def add_done_callback(self, fn): # pylint: disable=invalid-name raise NotImplementedError() @abc.abstractmethod def set_result(self, result): raise NotImplementedError() @abc.abstractmethod def set_exception(self, exception): raise NotImplementedError() async_future.py 0000644 00000012353 15030317645 0007635 0 ustar 00 # Copyright 2020, Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """AsyncIO implementation of the abstract base Future class.""" import asyncio from google.api_core import exceptions from google.api_core import retry from google.api_core import retry_async from google.api_core.future import base class _OperationNotComplete(Exception): """Private exception used for polling via retry.""" pass RETRY_PREDICATE = retry.if_exception_type( _OperationNotComplete, exceptions.TooManyRequests, exceptions.InternalServerError, exceptions.BadGateway, ) DEFAULT_RETRY = retry_async.AsyncRetry(predicate=RETRY_PREDICATE) class AsyncFuture(base.Future): """A Future that polls peer service to self-update. The :meth:`done` method should be implemented by subclasses. The polling behavior will repeatedly call ``done`` until it returns True. .. note:: Privacy here is intended to prevent the final class from overexposing, not to prevent subclasses from accessing methods. Args: retry (google.api_core.retry.Retry): The retry configuration used when polling. This can be used to control how often :meth:`done` is polled. Regardless of the retry's ``deadline``, it will be overridden by the ``timeout`` argument to :meth:`result`. """ def __init__(self, retry=DEFAULT_RETRY): super().__init__() self._retry = retry self._future = asyncio.get_event_loop().create_future() self._background_task = None async def done(self, retry=DEFAULT_RETRY): """Checks to see if the operation is complete. Args: retry (google.api_core.retry.Retry): (Optional) How to retry the RPC. Returns: bool: True if the operation is complete, False otherwise. """ # pylint: disable=redundant-returns-doc, missing-raises-doc raise NotImplementedError() async def _done_or_raise(self): """Check if the future is done and raise if it's not.""" result = await self.done() if not result: raise _OperationNotComplete() async def running(self): """True if the operation is currently running.""" result = await self.done() return not result async def _blocking_poll(self, timeout=None): """Poll and await for the Future to be resolved. Args: timeout (int): How long (in seconds) to wait for the operation to complete. If None, wait indefinitely. """ if self._future.done(): return retry_ = self._retry.with_timeout(timeout) try: await retry_(self._done_or_raise)() except exceptions.RetryError: raise asyncio.TimeoutError( "Operation did not complete within the designated " "timeout." ) async def result(self, timeout=None): """Get the result of the operation. Args: timeout (int): How long (in seconds) to wait for the operation to complete. If None, wait indefinitely. Returns: google.protobuf.Message: The Operation's result. Raises: google.api_core.GoogleAPICallError: If the operation errors or if the timeout is reached before the operation completes. """ await self._blocking_poll(timeout=timeout) return self._future.result() async def exception(self, timeout=None): """Get the exception from the operation. Args: timeout (int): How long to wait for the operation to complete. If None, wait indefinitely. Returns: Optional[google.api_core.GoogleAPICallError]: The operation's error. """ await self._blocking_poll(timeout=timeout) return self._future.exception() def add_done_callback(self, fn): """Add a callback to be executed when the operation is complete. If the operation is completed, the callback will be scheduled onto the event loop. Otherwise, the callback will be stored and invoked when the future is done. Args: fn (Callable[Future]): The callback to execute when the operation is complete. """ if self._background_task is None: self._background_task = asyncio.get_event_loop().create_task( self._blocking_poll() ) self._future.add_done_callback(fn) def set_result(self, result): """Set the Future's result.""" self._future.set_result(result) def set_exception(self, exception): """Set the Future's exception.""" self._future.set_exception(exception) __pycache__/__init__.cpython-310.pyc 0000644 00000000501 15030317645 0013214 0 ustar 00 o �h� � @ s d Z ddlmZ dgZdS )z1Futures for dealing with asynchronous operations.� )�Futurer N)�__doc__�google.api_core.future.baser �__all__� r r �R/usr/local/CyberCP/lib/python3.10/site-packages/google/api_core/future/__init__.py�<module> s __pycache__/async_future.cpython-310.pyc 0000644 00000012401 15030317645 0014166 0 ustar 00 o �h� � @ s� d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ G dd� de�Z e� e ejejej �Zejed �ZG d d� dej�ZdS )z9AsyncIO implementation of the abstract base Future class.� N)� exceptions)�retry)�retry_async)�basec @ s e Zd ZdZdS )�_OperationNotCompletez-Private exception used for polling via retry.N)�__name__� __module__�__qualname__�__doc__� r r �V/usr/local/CyberCP/lib/python3.10/site-packages/google/api_core/future/async_future.pyr s r )� predicatec sv e Zd ZdZef� fdd� Zefdd�Zdd� Zdd � Zddd�Z dd d�Z ddd�Zdd� Zdd� Z dd� Z� ZS )�AsyncFuturea� A Future that polls peer service to self-update. The :meth:`done` method should be implemented by subclasses. The polling behavior will repeatedly call ``done`` until it returns True. .. note:: Privacy here is intended to prevent the final class from overexposing, not to prevent subclasses from accessing methods. Args: retry (google.api_core.retry.Retry): The retry configuration used when polling. This can be used to control how often :meth:`done` is polled. Regardless of the retry's ``deadline``, it will be overridden by the ``timeout`` argument to :meth:`result`. c s( t � �� || _t�� �� | _d | _d S �N)�super�__init__�_retry�asyncio�get_event_loop� create_future�_future�_background_task��selfr �� __class__r r r : s zAsyncFuture.__init__c � s �t � �)z�Checks to see if the operation is complete. Args: retry (google.api_core.retry.Retry): (Optional) How to retry the RPC. Returns: bool: True if the operation is complete, False otherwise. )�NotImplementedErrorr r r r �done@ s � zAsyncFuture.donec � s �| � � I dH }|s t� �dS )z2Check if the future is done and raise if it's not.N)r r �r �resultr r r �_done_or_raiseL s ��zAsyncFuture._done_or_raisec � s �| � � I dH }| S )z+True if the operation is currently running.N)r r r r r �runningR s �zAsyncFuture.runningNc � sP �| j �� rdS | j�|�}z|| j�� I dH W dS tjy' t�d��w )z�Poll and await for the Future to be resolved. Args: timeout (int): How long (in seconds) to wait for the operation to complete. If None, wait indefinitely. Nz9Operation did not complete within the designated timeout.) r r r �with_timeoutr r � RetryErrorr �TimeoutError)r �timeout�retry_r r r �_blocking_pollW s � ��zAsyncFuture._blocking_pollc � � �| j |d�I dH | j�� S )a� Get the result of the operation. Args: timeout (int): How long (in seconds) to wait for the operation to complete. If None, wait indefinitely. Returns: google.protobuf.Message: The Operation's result. Raises: google.api_core.GoogleAPICallError: If the operation errors or if the timeout is reached before the operation completes. �r% N)r'