File: //lib64/python3.6/site-packages/tornado/__pycache__/locks.cpython-36.pyc
3
��f�D � @ s d dl Z d dlmZ d dlZd dlZd dlmZmZ d dlm Z m
Z
d dlmZm
Z
mZmZmZ d dlZejr~d dlmZmZ ddd d
dgZG dd
� d
e�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd � d e�ZG dd
� d
e�ZG dd� de�ZdS )� N)�CancelledError)�gen�ioloop)�Future�"future_set_result_unless_cancelled)�Union�Optional�Type�Any� Awaitable)�Deque�Set� Condition�Event� Semaphore�BoundedSemaphore�Lockc @ s, e Zd ZdZdd�dd�Zdd�dd�ZdS )�_TimeoutGarbageCollectorz�Base class for objects that periodically clean up timed-out waiters.
Avoids memory leak in a common pattern like:
while True:
yield condition.wait(short_timeout)
print('looping....')
N)�returnc C s t j� | _d| _d S )Nr )�collections�deque�_waiters� _timeouts)�self� r �/usr/lib64/python3.6/locks.py�__init__* s
z!_TimeoutGarbageCollector.__init__c C s: | j d7 _ | j dkr6d| _ tjdd� | jD ��| _d S )N� �d r c s s | ]}|j � s|V qd S )N)�done)�.0�wr r r � <genexpr>3 s z<_TimeoutGarbageCollector._garbage_collect.<locals>.<genexpr>)r r r r )r r r r �_garbage_collect. s
z)_TimeoutGarbageCollector._garbage_collect)�__name__�
__module__�__qualname__�__doc__r r# r r r r r s r c st e Zd ZdZdd�� fdd�Zed�dd�Zdeee j
f ee d�d d
�Z
dedd�d
d�Zdd�dd�Z� ZS )r a� A condition allows one or more coroutines to wait until notified.
Like a standard `threading.Condition`, but does not need an underlying lock
that is acquired and released.
With a `Condition`, coroutines can wait to be notified by other coroutines:
.. testcode::
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.locks import Condition
condition = Condition()
async def waiter():
print("I'll wait right here")
await condition.wait()
print("I'm done waiting")
async def notifier():
print("About to notify")
condition.notify()
print("Done notifying")
async def runner():
# Wait for waiter() and notifier() in parallel
await gen.multi([waiter(), notifier()])
IOLoop.current().run_sync(runner)
.. testoutput::
I'll wait right here
About to notify
Done notifying
I'm done waiting
`wait` takes an optional ``timeout`` argument, which is either an absolute
timestamp::
io_loop = IOLoop.current()
# Wait up to 1 second for a notification.
await condition.wait(timeout=io_loop.time() + 1)
...or a `datetime.timedelta` for a timeout relative to the current time::
# Wait up to 1 second.
await condition.wait(timeout=datetime.timedelta(seconds=1))
The method returns False if there's no notification before the deadline.
.. versionchanged:: 5.0
Previously, waiters could be notified synchronously from within
`notify`. Now, the notification will always be received on the
next iteration of the `.IOLoop`.
N)r c s t t| �j� tjj� | _d S )N)�superr r r �IOLoop�current�io_loop)r )� __class__r r r r s zCondition.__init__c C s. d| j jf }| jr&|dt| j� 7 }|d S )Nz<%sz waiters[%s]�>)r, r$ r �len)r �resultr r r �__repr__v s zCondition.__repr__)�timeoutr c sX t � ��jj�� |rTdd���fdd�}tjj� � � j||���j� �fdd�� �S )z�Wait for `.notify`.
Returns a `.Future` that resolves ``True`` if the condition is notified,
or ``False`` after a timeout.
N)r c s �j � st�d� � j� d S )NF)r r r# r )r �waiterr r �
on_timeout� s
z"Condition.wait.<locals>.on_timeoutc s
� j ��S )N)�remove_timeout)�_)r+ �timeout_handler r �<lambda>� s z Condition.wait.<locals>.<lambda>)r r �appendr r) r* �add_timeout�add_done_callback)r r1 r3 r )r+ r r6 r2 r �wait| s
zCondition.waitr )�nr c C sT g }x2|r6| j r6| j j� }|j� s|d8 }|j|� qW x|D ]}t|d� q>W dS )zWake ``n`` waiters.r TN)r �popleftr r8 r )r r<