U
    Ã@Ûfì  ã                   @   s|   d Z ddlZzddlZdZW n ek
r4   dZY nX ddlZddlZddlm	Z	 ddl
mZ dd„ ZG d	d
„ d
e	jƒZdS )z?
Asyncio-based hub, originally implemented by Miguel Grinberg.
é    NTF)Úhub)Úoriginalc                   C   s   dS )z˜
    Indicate whether this hub is available, since some hubs are
    platform-specific.

    Python always has asyncio, so this is always ``True``.
    T© r   r   r   úO/var/www/html/chatgpt/venv/lib/python3.8/site-packages/eventlet/hubs/asyncio.pyÚis_available   s    r   c                       s`   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Z‡ fdd	„Z‡ fd
d„Z‡ fdd„Z	dd„ Z
‡  ZS )ÚHubz?An Eventlet hub implementation on top of an asyncio event loop.c                    st   t ƒ  ¡  tr*tdƒtjj_tdƒtjj_dd l	}tdƒ|_
dd l}tdƒ|j_| ¡ | _| | j¡ | ¡ | _d S )NÚ	threadingÚqueuer   ÚselectÚsocket)ÚsuperÚ__init__Úconcurrent_importedr   Ú
concurrentÚfuturesÚthreadr   r	   Ú	selectorsr
   Zasyncio.base_eventsÚbase_eventsr   Únew_event_loopÚloopÚset_event_loopÚEventÚsleep_event)Úselfr   Úasyncio©Ú	__class__r   r   r   !   s    


zHub.__init__c                    s   t ƒ  |¡ | j ¡  dS )zX
        Register a ``Timer``.

        Typically not called directly by users.
        N)r   Ú	add_timerr   Úset)r   Útimerr   r   r   r   7   s    zHub.add_timerc                 C   sN   z||ƒ W n2 | j k
r$   ‚ Y n   |  |t ¡ ¡ Y nX | j ¡  dS )zU
        Callback called by ``asyncio`` when a file descriptor has an event.
        N)ZSYSTEM_EXCEPTIONSZsquelch_exceptionÚsysÚexc_infor   r   )r   ÚcbÚfilenor   r   r   Ú_file_cb@   s    zHub._file_cbc                    s   zt  |¡ W n tk
r*   tdƒ‚Y nX | j|  |¡dk	}tƒ  |||||¡}|sŒ|tj	krx| j
 || j||¡ n| j
 || j||¡ |S )z¥
        Add a file descriptor of given event type to the ``Hub``.  See the
        superclass for details.

        Typically not called directly by users.
        zInvalid file descriptorN)ÚosÚfstatÚOSErrorÚ
ValueErrorÚ	listenersÚgetr   Úaddr   ÚREADr   Ú
add_readerr$   Ú
add_writer)r   Úevtyper#   r"   ÚtbZmark_as_closedZalready_listeningÚlistenerr   r   r   r+   L   s    
zHub.addc                    sP   t ƒ  |¡ |j}|j}| j|  |¡sL|tjkr@| j 	|¡ n| j 
|¡ dS )z‡
        Remove a listener from the ``Hub``.  See the superclass for details.

        Typically not called directly by users.
        N)r   Úremover/   r#   r)   r*   r   r,   r   Úremove_readerÚremove_writer)r   r1   r/   r#   r   r   r   r2   `   s    
z
Hub.removec                    sT   | j tj  |¡}| j tj  |¡}tƒ  |¡ |r@| j |¡ |rP| j 	|¡ dS )zv
        Remove a file descriptor from the ``asyncio`` loop.

        Typically not called directly by users.
        N)
r)   r   r,   r*   ÚWRITEr   Úremove_descriptorr   r3   r4   )r   r#   Z	have_readZ
have_writer   r   r   r6   o   s    zHub.remove_descriptorc                    s   ‡ fdd„}ˆ j  |ƒ ¡ dS )zL
        Start the ``Hub`` running. See the superclass for details.
        c                  “   s   ˆ j rtdƒ‚zþdˆ _ dˆ _ˆ jsìˆ jr2ˆ  ¡  q"ˆ  ¡  ˆ jrHˆ  ¡  ˆ  ˆ  	¡ ¡ ˆ jrdˆ  
¡  ˆ  ¡  ˆ  ¡ } | d kr†ˆ  ¡ }n| ˆ  	¡  }|dkrÚzt ˆ j ¡ |¡I d H  W n tjk
rÌ   Y nX ˆ j ¡  qt d¡I d H  qdˆ _ˆ jd d …= ˆ jd d …= W 5 dˆ _ dˆ _X d S )NzAlready running!FTr   )ÚrunningÚRuntimeErrorZstoppingÚclosedZ	close_oneZprepare_timersZdebug_blockingZblock_detect_preZfire_timersÚclockZblock_detect_postZsleep_untilZdefault_sleepr   Úwait_forr   ÚwaitÚTimeoutErrorÚclearÚsleepZtimers_canceledZtimersZnext_timers)Zwakeup_whenZ
sleep_time©r   r   r   Ú	async_run   sB    

ÿzHub.run.<locals>.async_runN)r   Úrun_until_complete)r   ÚaÚkwrA   r   r@   r   Úrun}   s    'zHub.run)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r+   r2   r6   rE   Ú__classcell__r   r   r   r   r      s   	r   )rI   r   Zconcurrent.futures.threadr   r   r8   r%   r    Zeventlet.hubsr   Zeventlet.patcherr   r   ZBaseHubr   r   r   r   r   Ú<module>   s   

