a
    õ£Be,  ã                	   @   sÒ   U d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZ dZeed< zddlZejZW n eefyv   Y n0 ddlmZmZ dd	lmZ G d
d„ deƒZG dd„ dƒZG dd„ deƒZeZdd„ ZdS )uX  tornado IOLoop API with zmq compatibility

If you have tornado â‰¥ 3.0, this is a subclass of tornado's IOLoop,
otherwise we ship a minimal subset of tornado in zmq.eventloop.minitornado.

The minimal shipped version of tornado's IOLoop does not include
support for concurrent futures - this will only be available if you
have tornado â‰¥ 3.0.
é    N)ÚTuple)ÚETERMÚPOLLERRÚPOLLINÚPOLLOUTÚPollerÚZMQError© Útornado_versioné   )ÚPeriodicCallbackÚ
PollIOLoop)Úgen_logc                       s2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚDelayedCallbackzöSchedules the given callback to be called once.

    The callback is called once, after callback_time milliseconds.

    `start` must be called after the DelayedCallback is created.

    The timeout is calculated from when `start` is called.
    Nc                    s*   t  dt¡ t|dƒ}tƒ  |||¡ d S )NzDDelayedCallback is deprecated.
        Use loop.add_timeout instead.gü©ñÒMbP?)ÚwarningsÚwarnÚDeprecationWarningÚmaxÚsuperÚ__init__)ÚselfÚcallbackÚcallback_timeÚio_loop©Ú	__class__r	   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/zmq/eventloop/_deprecated.pyr   +   s    ý
zDelayedCallback.__init__c                 C   s6   d| _ d| _t ¡ | jd  | _| j | j| j¡ dS )zStarts the timer.Tg     @�@N)Ú_runningZ	_firstrunÚtimer   Z_next_timeoutr   Zadd_timeoutÚ_run©r   r	   r	   r   Ústart5   s    zDelayedCallback.startc                 C   sB   | j s
d S d| _ z|  ¡  W n  ty<   tjddd� Y n0 d S )NFzError in delayed callbackT)Úexc_info)r   r   Ú	Exceptionr   Úerrorr    r	   r	   r   r   <   s    zDelayedCallback._run)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   Ú__classcell__r	   r	   r   r   r   !   s   	
r   c                   @   sX   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )Ú	ZMQPollerz¶A poller that can be used in the tornado IOLoop.

    This simply wraps a regular zmq.Poller, scaling the timeout
    by 1000, so that it is in seconds rather than milliseconds.
    c                 C   s   t ƒ | _d S ©N)r   Ú_pollerr    r	   r	   r   r   M   s    zZMQPoller.__init__c                 C   s>   d}| t j@ r|tO }| t j@ r(|tO }| t j@ r:|tO }|S )zEtranslate IOLoop.READ/WRITE/ERROR event masks into zmq.POLLIN/OUT/ERRr   )ÚIOLoopÚREADr   ÚWRITEr   ÚERRORr   )ÚeventsÚz_eventsr	   r	   r   Ú_map_eventsP   s    


zZMQPoller._map_eventsc                 C   s>   d}| t @ r|tjO }| t@ r(|tjO }| t@ r:|tjO }|S )zEtranslate zmq.POLLIN/OUT/ERR event masks into IOLoop.READ/WRITE/ERRORr   )r   r-   r.   r   r/   r   r0   )r2   r1   r	   r	   r   Ú_remap_events\   s    


zZMQPoller._remap_eventsc                 C   s   | j  ||  |¡¡S r+   )r,   Úregisterr3   ©r   Úfdr1   r	   r	   r   r5   h   s    zZMQPoller.registerc                 C   s   | j  ||  |¡¡S r+   )r,   Úmodifyr3   r6   r	   r	   r   r8   k   s    zZMQPoller.modifyc                 C   s   | j  |¡S r+   )r,   Ú
unregister)r   r7   r	   r	   r   r9   n   s    zZMQPoller.unregisterc                    s"   ˆ j  d| ¡}‡ fdd„|D ƒS )zgpoll in seconds rather than milliseconds.

        Event masks will be IOLoop.READ/WRITE/ERROR
        iè  c                    s   g | ]\}}|ˆ   |¡f‘qS r	   )r4   )Ú.0r7   Zevtr    r	   r   Ú
<listcomp>w   ó    z"ZMQPoller.poll.<locals>.<listcomp>)r,   Úpoll)r   Útimeoutr2   r	   r    r   r=   q   s    zZMQPoller.pollc                 C   s   d S r+   r	   r    r	   r	   r   Úclosey   s    zZMQPoller.closeN)r%   r&   r'   r(   r   Ústaticmethodr3   r4   r5   r8   r9   r=   r?   r	   r	   r	   r   r*   F   s   

r*   c                       sJ   e Zd ZdZeZd‡ fdd„	Zedd„ ƒZedd„ ƒZ	‡ fd	d
„Z
‡  ZS )Ú	ZMQIOLoopzfZMQ subclass of tornado's IOLoop

    Minor modifications, so that .current/.instance return self
    Nc                    s.   |d u r|   ¡ n|}tƒ jf d|i|¤Ž d S )NÚimpl)Ú	_zmq_implr   Ú
initialize)r   rB   Úkwargsr   r	   r   rD   …   s    zZMQIOLoop.initializec                 O   sL   t dkrt | ¡ tj|i |¤Ž}t|| ƒsHtjd| ›d|›�tdd� |S )a  Returns a global `IOLoop` instance.

        Most applications have a single, global `IOLoop` running on the
        main thread.  Use this method to get this instance from
        another thread.  To get the current thread's `IOLoop`, use `current()`.
        ©é   ú$IOLoop.current expected instance of ú, got é   ©Ú
stacklevel)r
   r   Ú	configureÚinstanceÚ
isinstancer   r   ÚRuntimeWarning©ÚclsÚargsrE   Úloopr	   r	   r   rN   ‰   s    


ýzZMQIOLoop.instancec                 O   sL   t dkrt | ¡ tj|i |¤Ž}t|| ƒsHtjd| ›d|›�tdd� |S )u&   Returns the current threadâ€™s IOLoop.rF   rH   rI   rJ   rK   )r
   r   rM   ÚcurrentrO   r   r   rP   rQ   r	   r	   r   rU   ž   s    

ýzZMQIOLoop.currentc              
      sF   zt ƒ  ¡  W n2 ty@ } z|jtkr*n‚ W Y d }~n
d }~0 0 d S r+   )r   r!   r   Úerrnor   )r   Úer   r	   r   r!   ®   s    
zZMQIOLoop.start)N)r%   r&   r'   r(   r*   rC   rD   ÚclassmethodrN   rU   r!   r)   r	   r	   r   r   rA   }   s   

rA   c                  C   sV   ddl m}  | j ¡ r0| j ¡ t ¡ u s0J dƒ‚tdkrF| j t¡ nt ¡ | j_dS )a,  set the tornado IOLoop instance with the pyzmq IOLoop.

    After calling this function, tornado's IOLoop.instance() and pyzmq's
    IOLoop.instance() will return the same object.

    An assertion error will be raised if tornado's IOLoop has been initialized
    prior to calling this function.
    r   ©Úioloopz"tornado IOLoop already initializedrF   N)	ÚtornadorZ   r-   ZinitializedrN   r
   rM   rA   Z	_instancerY   r	   r	   r   Úinstall½   s    	ÿþýr\   )r(   r   r   Útypingr   Zzmqr   r   r   r   r   r   r
   Ú__annotations__r[   Úversion_infoÚImportErrorÚAttributeErrorZminitornado.ioloopr   r   Zminitornado.logr   r   r*   rA   r-   r\   r	   r	   r	   r   Ú<module>   s"    
%7=