File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/asgiref.tar
Back
timeout.py 0000644 00000007053 15030103312 0006575 0 ustar 00 # This code is originally sourced from the aio-libs project "async_timeout", # under the Apache 2.0 license. You may see the original project at # https://github.com/aio-libs/async-timeout # It is vendored here to reduce chain-dependencies on this library, and # modified slightly to remove some features we don't use. import asyncio import warnings from types import TracebackType from typing import Any # noqa from typing import Optional, Type class timeout: """timeout context manager. Useful in cases when you want to apply timeout logic around block of code or in cases when asyncio.wait_for is not suitable. For example: >>> with timeout(0.001): ... async with aiohttp.get('https://github.com') as r: ... await r.text() timeout - value in seconds or None to disable timeout logic loop - asyncio compatible event loop """ def __init__( self, timeout: Optional[float], *, loop: Optional[asyncio.AbstractEventLoop] = None, ) -> None: self._timeout = timeout if loop is None: loop = asyncio.get_running_loop() else: warnings.warn( """The loop argument to timeout() is deprecated.""", DeprecationWarning ) self._loop = loop self._task = None # type: Optional[asyncio.Task[Any]] self._cancelled = False self._cancel_handler = None # type: Optional[asyncio.Handle] self._cancel_at = None # type: Optional[float] def __enter__(self) -> "timeout": return self._do_enter() def __exit__( self, exc_type: Type[BaseException], exc_val: BaseException, exc_tb: TracebackType, ) -> Optional[bool]: self._do_exit(exc_type) return None async def __aenter__(self) -> "timeout": return self._do_enter() async def __aexit__( self, exc_type: Type[BaseException], exc_val: BaseException, exc_tb: TracebackType, ) -> None: self._do_exit(exc_type) @property def expired(self) -> bool: return self._cancelled @property def remaining(self) -> Optional[float]: if self._cancel_at is not None: return max(self._cancel_at - self._loop.time(), 0.0) else: return None def _do_enter(self) -> "timeout": # Support Tornado 5- without timeout # Details: https://github.com/python/asyncio/issues/392 if self._timeout is None: return self self._task = asyncio.current_task(self._loop) if self._task is None: raise RuntimeError( "Timeout context manager should be used " "inside a task" ) if self._timeout <= 0: self._loop.call_soon(self._cancel_task) return self self._cancel_at = self._loop.time() + self._timeout self._cancel_handler = self._loop.call_at(self._cancel_at, self._cancel_task) return self def _do_exit(self, exc_type: Type[BaseException]) -> None: if exc_type is asyncio.CancelledError and self._cancelled: self._cancel_handler = None self._task = None raise asyncio.TimeoutError if self._timeout is not None and self._cancel_handler is not None: self._cancel_handler.cancel() self._cancel_handler = None self._task = None return None def _cancel_task(self) -> None: if self._task is not None: self._task.cancel() self._cancelled = True __pycache__/testing.cpython-310.pyc 0000644 00000006140 15030103312 0013117 0 ustar 00 o �h� � @ sB d dl Z d dlZd dlZddlmZ ddlmZ G dd� d�ZdS )� N� )�guarantee_single_callable)�timeoutc @ sP e Zd ZdZdd� Zddd�Zddd �Zd d� Zdd � Zddd�Z ddd�Z dS )�ApplicationCommunicatorz} Runs an ASGI application in a test mode, allowing sending of messages to it and retrieval of messages it sends. c C sN t |�| _|| _t�� | _t�� | _t�� � tj | �|| jj| jj��| _ d S �N)r �application�scope�asyncio�Queue�input_queue�output_queue�contextvars�Context�run�create_task�get�put�future)�selfr r � r �B/usr/local/CyberCP/lib/python3.10/site-packages/asgiref/testing.py�__init__ s �z ApplicationCommunicator.__init__r c � s� �zXt |�4 I dH �# z | jI dH | j�� W n tjy" Y nw W d �I dH n1 I dH s3w Y W | j�� sX| j�� z | jI dH W dS tjyW Y dS w dS | j�� sw| j�� z| jI dH W w tjyv Y w w w )zV Waits for the application to stop itself and returns any exceptions. N)� async_timeoutr �resultr �CancelledError�done�cancel)r r r r r �wait s6 ���(�� �� ��zApplicationCommunicator.waitTc C s. | j �� s| j �� d S |r| j �� d S d S r )r r r r )r � exceptionsr r r �stop0 s �zApplicationCommunicator.stopc C s( z | j dd� W d S ty Y d S w )NF)r )r �RuntimeError)r r r r �__del__7 s �zApplicationCommunicator.__del__c � s �| j �|�I dH dS )z; Sends a single message to the application N)r r )r �messager r r � send_input? s �z"ApplicationCommunicator.send_inputc � s� �| j �� r| j �� z*t|�4 I dH � | j�� I dH W d �I dH W S 1 I dH s.w Y W dS tjyh } z&| j �� rJ| j �� |�| j �� z | j I dH W |� tj yc Y |�w d}~ww )zX Receives a single message from the application, with optional timeout. N) r r r r r r r �TimeoutErrorr r )r r �er r r �receive_outputF s* � 4� �����z&ApplicationCommunicator.receive_output皙�����?�{�G�z�?c � sR �t �� }t �� | |k r$| j�� sdS t�|�I dH t �� | |k s | j�� S )zO Checks that there is no message to receive in the given time. FN)�time� monotonicr �emptyr �sleep)r r �interval�startr r r �receive_nothing] s � � z'ApplicationCommunicator.receive_nothingN)r )T)r'