B
iIa? @ s d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm Z ej
dkrXd dlmZ nd dlmZ dZ
dZee dd d
dZee ddd
dZG dd dejZeG dd dZej
dkre jddddZne jddddZej
dkre jdddZne jdddZdS ) N)
TracebackType)AnyOptionalType) )finalz4.0.2)timeout
timeout_atTimeoutr )delayreturnc C s* t }| dk r| | }nd}t||S )ar 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:
>>> async with timeout(0.001):
... async with aiohttp.get('https://github.com') as r:
... await r.text()
delay - value in seconds or None to disable timeout logic
N)_get_running_looptimer )r loopdeadline r G/opt/alt/python37/lib/python3.7/site-packages/async_timeout/__init__.pyr s
r )r r
c C s t }t| |S )a Schedule the timeout at absolute time.
deadline argument points on the time in the same clock system
as loop.time().
Please note: it is not POSIX time but a time with
undefined starting base, e.g. the time of the system power on.
>>> async with timeout_at(loop.time() + 10):
... async with aiohttp.get('https://github.com') as r:
... await r.text()
)r r )r r r r r r
* s r
c @ s e Zd ZdZdZdZdZdS )_StateINITENTERTIMEOUTEXITN)__name__
__module____qualname__r r r r r r r r r = s r c @ s< e Zd ZdZee ejddddZd dddZ ee
e ee ee ee
d d
dZd ddd
Zee
e ee ee ee
d ddZee
dddZeee dddZddddZddddZeddddZeddddZddddZddd d!Zee
e dd"d#d$Zd%dd&d'd(ZdS ))r ) _deadline_loop_state_timeout_handlerN)r r r
c C s2 || _ tj| _d | _|d kr$d | _n
| | d S )N)r r r r r r update)selfr r r r r __init__Y s zTimeout.__init__)r
c C s t jdtdd | | S )Nz>with timeout() is deprecated, use async with timeout() instead )
stacklevel)warningswarnDeprecationWarning _do_enter)r! r r r __enter__e s zTimeout.__enter__)exc_typeexc_valexc_tbr
c C s | | d S )N)_do_exit)r! r* r+ r, r r r __exit__n s
zTimeout.__exit__c s | | S )N)r( )r! r r r
__aenter__w s zTimeout.__aenter__c s | | d S )N)r- )r! r* r+ r, r r r __aexit__{ s
zTimeout.__aexit__c C s | j tjkS )z$Is timeout expired during execution?)r r r )r! r r r expired s zTimeout.expiredc C s | j S )N)r )r! r r r r s zTimeout.deadlinec C s0 | j tjtjfkr$td| j j | dS )z Reject scheduled timeout if any.zinvalid state N)r r r r RuntimeErrorvalue_reject)r! r r r reject s zTimeout.rejectc C s | j d k r| j d | _ d S )N)r cancel)r! r r r r4 s
zTimeout._reject)r r
c C s( | j }|dkrtd| || dS )zAdvance timeout on delay seconds.
The delay can be negative.
Raise RuntimeError if shift is called when deadline is not scheduled
Nz1cannot shift timeout if deadline is not scheduled)r r2 r )r! r r r r r shift s z
Timeout.shift)r r
c C sZ | j tjkrtd| j tjkr(td| jdk r<| j || _| j tjkrV| dS )aU Set deadline to absolute value.
deadline argument points on the time in the same clock system
as loop.time().
If new deadline is in the past the timeout is raised immediatelly.
Please note: it is not POSIX time but a time with
undefined starting base, e.g. the time of the system power on.
z1cannot reschedule after exit from context managerz!cannot reschedule expired timeoutN)
r r r r2 r r r6 r r _reschedule)r! r r r r r s
zTimeout.updatec C s~ | j tjkst| j}|d kr"d S | j }| jd k r@| j t | j}||krf| j
| j|| _n| j|| j|| _d S )N)
r r r AssertionErrorr r r r r6
_current_taskZ call_soon_on_timeoutZcall_at)r! r Znowtaskr r r r8 s
zTimeout._reschedulec C s2 | j tjkrtd| j j tj| _ | d S )Nzinvalid state )r r r r2 r3 r r8 )r! r r r r( s zTimeout._do_enter)r* r
c C s6 |t jkr"| jtjkr"d | _t jtj| _| d S )N) asyncioZCancelledErrorr r r r TimeoutErrorr r4 )r! r* r r r r- s zTimeout._do_exitzasyncio.Task[None])r<