U
    RhW                     @  st  U d dl mZ d dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZmZ d dlZejdkrd dlmZmZ nd dlmZmZ erd	d
lmZ dZedZedZe Zi Zded< dddddddddddZdddddZ dddd Z!ddd!d"d#Z"ddd$d%Z#d&dd'd(Z$d)dd*d+Z%e
d,d-d.d/d0d1Z&d6d2d,d3d4d5Z'dS )7    )annotationsN)	AwaitableCallable	Generator)contextmanager)import_module)TYPE_CHECKINGAnyTypeVar)      )TypeVarTupleUnpack   )AsyncBackend)asyncioZtrioT_RetvalPosArgsTzdict[str, type[AsyncBackend]]loaded_backendsr   )backendbackend_optionsz1Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]]zUnpack[PosArgsT]strzdict[str, Any] | None)funcargsr   r   returnc             
   G  s   zt  }W n t jk
r"   Y nX td| dzt|}W n2 tk
rr } ztd| |W 5 d}~X Y nX d}t jddkrt j	|}z|pi }|| |i |W S |rt j
| X dS )u  
    Run the given coroutine function in an asynchronous event loop.

    The current thread must not be already running an event loop.

    :param func: a coroutine function
    :param args: positional arguments to ``func``
    :param backend: name of the asynchronous event loop implementation – currently
        either ``asyncio`` or ``trio``
    :param backend_options: keyword arguments to call the backend ``run()``
        implementation with (documented :ref:`here <backend options>`)
    :return: the return value of the coroutine function
    :raises RuntimeError: if an asynchronous event loop is already running in this
        thread
    :raises LookupError: if the named backend is not found

    zAlready running z in this threadzNo such backend: N)sniffiocurrent_async_libraryZAsyncLibraryNotFoundErrorRuntimeErrorget_async_backendImportErrorLookupErrorZcurrent_async_library_cvargetsetresetrun)r   r   r   r   asynclib_nameZasync_backendexctoken r(   <./venv/lib/python3.8/site-packages/anyio/_core/_eventloop.pyr$      s"    "r$   floatNone)delayr   c                   s   t  | I dH S )zi
    Pause the current task for the specified duration.

    :param delay: the duration, in seconds

    N)r   sleep)r,   r(   r(   r)   r-   P   s    r-   )r   c                     s   t tjI dH  dS )z
    Pause the current task until it's cancelled.

    This is a shortcut for ``sleep(math.inf)``.

    .. versionadded:: 3.1

    N)r-   mathinfr(   r(   r(   r)   sleep_foreverZ   s    	r0   )deadliner   c                   s"   t  }tt| | dI dH  dS )z
    Pause the current task until the given time.

    :param deadline: the absolute time to wake up at (according to the internal
        monotonic clock of the event loop)

    .. versionadded:: 3.1

    r   N)current_timer-   max)r1   Znowr(   r(   r)   sleep_untilf   s    
r4   c                   C  s
   t   S )zo
    Return the current value of the event loop's internal clock.

    :return: the clock value (seconds)

    )r   r2   r(   r(   r(   r)   r2   t   s    r2   ztuple[str, ...]c                   C  s   t S )z5Return a tuple of the names of all built-in backends.)BACKENDSr(   r(   r(   r)   get_all_backends~   s    r6   ztype[BaseException]c                   C  s
   t   S )z@Return the current async library's cancellation exception class.)r   Zcancelled_exception_classr(   r(   r(   r)   get_cancelled_exc_class   s    r7   ztype[AsyncBackend]objectzGenerator[Any, None, None])backend_classr'   r   c                 c  s&   | t _|t _z
d V  W 5 t `t `X d S )N)threadlocalsZcurrent_async_backendcurrent_token)r9   r'   r(   r(   r)   claim_worker_thread   s    
r<   z
str | None)r%   r   c                 C  sR   | d krt  } z
t|  W S  tk
rL   td|  }|jt| < |j Y S X d S )Nzanyio._backends._)r   r   r   KeyErrorr   r9   )r%   moduler(   r(   r)   r      s    

r   )N)(Z
__future__r   r.   sys	threadingZcollections.abcr   r   r   
contextlibr   	importlibr   typingr   r	   r
   r   version_infor   r   Ztyping_extensionsabcr   r5   r   r   localr:   r   __annotations__r$   r-   r0   r4   r2   r6   r7   r<   r   r(   r(   r(   r)   <module>   s>    
1


