File: //usr/lib/python3/dist-packages/asgiref/__pycache__/timeout.cpython-310.pyc
o
�6�ap
� @ s: d dl Z d dlmZ d dlmZmZmZ G dd� d�ZdS )� N)�
TracebackType)�Any�Optional�Typec @ s� e Zd ZdZdd�d ee deej ddfdd�Zddd �Z d
e
e dededee
fd
d�Zddd�Zd
e
e dededdfdd�Zede
fdd��Zedee fdd��Zddd�Zd
e
e ddfdd�Zddd�ZdS )�timeouta� 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
N)�loopr �returnc C s8 || _ |d u rt�� }|| _d | _d| _d | _d | _d S )NF)�_timeout�asyncio�get_event_loop�_loop�_task�
_cancelled�_cancel_handler�
_cancel_at)�selfr r � r �1/usr/lib/python3/dist-packages/asgiref/timeout.py�__init__ s
ztimeout.__init__c C s | � � S �N�� _do_enter�r r r r � __enter__, s ztimeout.__enter__�exc_type�exc_val�exc_tbc C s | � |� d S r ��_do_exit�r r r r r r r �__exit__/ s
ztimeout.__exit__c � s
�| � � S r r r r r r �
__aenter__8 s �ztimeout.__aenter__c � s �| � |� d S r r r r r r � __aexit__; s �ztimeout.__aexit__c C s | j S r )r r r r r �expiredC s ztimeout.expiredc C s$ | j d urt| j | j�� d�S d S )Ng )r �maxr �timer r r r � remainingG s
ztimeout.remainingc C st | j d u r| S t�| j�| _| jd u rtd��| j dkr%| j�| j� | S | j�� | j | _ | j�
| j | j�| _| S )Nz4Timeout context manager should be used inside a taskr )r r
�current_taskr r
�RuntimeError� call_soon�_cancel_taskr% r �call_atr r r r r r N s
�
ztimeout._do_enterc C sP |t ju r| jrd | _d | _t j�| jd ur#| jd ur#| j�� d | _d | _d S r )r
�CancelledErrorr r r
�TimeoutErrorr �cancel)r r r r r r b s
ztimeout._do_exitc C s"