U
    ouf\                     @  s   U d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZmZmZ ddlmZ d	d
gZedeedf dZeddddd	ZdZded< edZG dd dZefdddddd
ZdS )z&
Implementation for async generators.
    )annotations)get_running_loop)asynccontextmanager)EmptyFullQueue)AnyAsyncGeneratorCallableIterableTypeVar   )run_in_executor_with_contextaclosinggenerator_to_async_generator_T_GeneratorN)boundz"AsyncGenerator[_T_Generator, None])thingreturnc                 C s    z
| V  W 5 |   I dH  X dS )z1Similar to `contextlib.aclosing`, in Python 3.10.N)aclose)r    r   L/tmp/pip-unpacked-wheel-8milen2s/prompt_toolkit/eventloop/async_generator.pyr      s    
i  intDEFAULT_BUFFER_SIZE_Tc                   @  s   e Zd ZdS )_DoneN)__name__
__module____qualname__r   r   r   r   r   0   s   r   zCallable[[], Iterable[_T]]zAsyncGenerator[_T, None])get_iterablebuffer_sizer   c                  s   dt |dt }dd fdd}t|}zPz }W n( tk
rh   |djI dH }Y nX t|trxqq4|V  q4W 5 d|I dH  X dS )	a]  
    Turn a generator or iterable into an async generator.

    This works by running the generator in a background thread.

    :param get_iterable: Function that returns a generator or iterable when
        called.
    :param buffer_size: Size of the queue between the async consumer and the
        synchronous generator that produces items.
    F)maxsizeNone)r   c                    s   dzb  D ]V} r W N dS zj | dd W q` tk
rZ   rRY  W  dS Y qY qX q
qq
W 5 zj t dd W q tk
r   rY   dS Y qfY qfX qqfX  dS )z~
        Consume the generator in background thread.
        When items are received, they'll be pushed to the queue.
        r   )timeoutN)putr   r   )itemr   qZquittingr   r   runnerG   s$    




z,generator_to_async_generator.<locals>.runnerTN)	r   r   r   
get_nowaitr   Zrun_in_executorget
isinstancer   )r   r    Zloopr(   Zrunner_fr%   r   r&   r   r   4   s    
"
)__doc__
__future__r   Zasyncior   
contextlibr   queuer   r   r   typingr   r	   r
   r   r   utilsr   __all__r   r   r   __annotations__r   r   r   r   r   r   r   <module>   s"   