a
    ވa                     @   s  d Z ddlmZmZmZmZ ddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZmZ ddlmZmZ ddlmZ ddlmZmZmZ zddl Z W n e!y   dZ Y n0 zddl"Z"W n e!y   ddl#Z"Y n0 dd	l$m%Z%m&Z& d
Z'G dd de(Z)G dd deZ*G dd de*Z+G dd de,Z-G dd de,Z.dS )a  An I/O event loop for non-blocking sockets.

Typical applications will use a single `IOLoop` object, in the
`IOLoop.instance` singleton.  The `IOLoop.start` method should usually
be called at the end of the ``main()`` function.  Atypical applications may
use more than one `IOLoop`, such as one `IOLoop` per thread, or per `unittest`
case.

In addition to I/O events, the `IOLoop` can also schedule time-based events.
`IOLoop.add_timeout` is a non-blocking alternative to `time.sleep`.
    )absolute_importdivisionprint_functionwith_statementN   )TracebackFuture	is_future)app_loggen_log)stack_context)Configurableerrno_from_exceptiontimedelta_to_seconds)set_close_execWakerg      @c                   @   s   e Zd ZdS )TimeoutErrorN)__name__
__module____qualname__ r   r   ?lib/python3.9/site-packages/zmq/eventloop/minitornado/ioloop.pyr   C   s   r   c                   @   s  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZeZeZeeB Ze Ze Zedd Zedd Zdd Zedd ZedRddZdd Zedd Zedd Zedd ZdSdd Z dTd"d#Z!d$d% Z"d&d' Z#d(d) Z$d*d+ Z%d,d- Z&d.d/ Z'd0d1 Z(d2d3 Z)d4d5 Z*dUd6d7Z+d8d9 Z,d:d; Z-d<d= Z.d>d? Z/d@dA Z0dBdC Z1dDdE Z2dFdG Z3dHdI Z4dJdK Z5dLdM Z6dNdO Z7dPdQ Z8dS )VIOLoopa  A level-triggered I/O loop.

    We use ``epoll`` (Linux) or ``kqueue`` (BSD and Mac OS X) if they
    are available, or else we fall back on select(). If you are
    implementing a system that needs to handle thousands of
    simultaneous connections, you should use a system that supports
    either ``epoll`` or ``kqueue``.

    Example usage for a simple TCP server:

    .. testcode::

        import errno
        import functools
        import tornado.ioloop
        import socket

        def connection_ready(sock, fd, events):
            while True:
                try:
                    connection, address = sock.accept()
                except socket.error as e:
                    if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
                        raise
                    return
                connection.setblocking(0)
                handle_connection(connection, address)

        if __name__ == '__main__':
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.setblocking(0)
            sock.bind(("", port))
            sock.listen(128)

            io_loop = tornado.ioloop.IOLoop.current()
            callback = functools.partial(connection_ready, sock)
            io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
            io_loop.start()

    .. testoutput::
       :hide:

    By default, a newly-constructed `IOLoop` becomes the thread's current
    `IOLoop`, unless there already is a current `IOLoop`. This behavior
    can be controlled with the ``make_current`` argument to the `IOLoop`
    constructor: if ``make_current=True``, the new `IOLoop` will always
    try to become current and it raises an error if there is already a
    current instance. If ``make_current=False``, the new `IOLoop` will
    not try to become current.

    .. versionchanged:: 4.2
       Added the ``make_current`` keyword argument to the `IOLoop`
       constructor.
    r               i    i   @l        r   c                   C   sH   t tdsBtj" t tds$t t_W d   n1 s80    Y  tjS )a1  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.  In most other cases, it is better to use `current()`
        to get the current thread's `IOLoop`.
        	_instanceN)hasattrr   _instance_lockr   r   r   r   r   instance   s
    	

&zIOLoop.instancec                   C   s
   t tdS )z8Returns true if the singleton instance has been created.r   )r   r   r   r   r   r   initialized   s    zIOLoop.initializedc                 C   s   t  rJ | t _dS )zInstalls this `IOLoop` object as the singleton instance.

        This is normally not necessary as `instance()` will create
        an `IOLoop` on demand, but you may want to call `install` to use
        a custom subclass of `IOLoop`.
        N)r   r    r   selfr   r   r   install   s    zIOLoop.installc                   C   s   t tdrt`dS )zKClear the global `IOLoop` instance.

        .. versionadded:: 4.0
        r   N)r   r   r   r   r   r   r   clear_instance   s    
zIOLoop.clear_instanceTc                 C   s&   t tjdd}|du r"| r"t S |S )a  Returns the current thread's `IOLoop`.

        If an `IOLoop` is currently running or has been marked as
        current by `make_current`, returns that instance.  If there is
        no current `IOLoop`, returns `IOLoop.instance()` (i.e. the
        main thread's `IOLoop`, creating one if necessary) if ``instance``
        is true.

        In general you should use `IOLoop.current` as the default when
        constructing an asynchronous object, and use `IOLoop.instance`
        when you mean to communicate to the main thread from a different
        one.

        .. versionchanged:: 4.1
           Added ``instance`` argument to control the fallback to
           `IOLoop.instance()`.
        r   N)getattrr   _currentr   )r   currentr   r   r   r'      s    zIOLoop.currentc                 C   s   | t j_dS )a  Makes this the `IOLoop` for the current thread.

        An `IOLoop` automatically becomes current for its thread
        when it is started, but it is sometimes useful to call
        `make_current` explicitly before starting the `IOLoop`,
        so that code run at startup time can find the right
        instance.

        .. versionchanged:: 4.1
           An `IOLoop` created while there is no current `IOLoop`
           will automatically become current.
        Nr   r&   r   r!   r   r   r   make_current   s    zIOLoop.make_currentc                   C   s   d t j_d S Nr(   r   r   r   r   clear_current   s    zIOLoop.clear_currentc                 C   s   t S r*   )r   )clsr   r   r   configurable_base   s    zIOLoop.configurable_basec                 C   s:   ddl m} |S ttdr*ddlm} |S ddlm	} |S )Nr   )	ZMQIOLoopZepoll)EPollIOLoopZkqueue)KQueueIOLoop)SelectIOLoop)
Zzmq.eventloop.ioloopr.   r   selectZtornado.platform.epollr/   Ztornado.platform.kqueuer0   Ztornado.platform.selectr1   )r,   r.   r/   r0   r1   r   r   r   configurable_default   s       
zIOLoop.configurable_defaultNc                 C   sJ   |d u r"t jddd u rF|   n$|rFt jddd ur>td|   d S )NF)r   zcurrent IOLoop already exists)r   r'   r)   RuntimeError)r"   r)   r   r   r   
initialize   s    
zIOLoop.initializeFc                 C   s
   t  dS )a  Closes the `IOLoop`, freeing any resources used.

        If ``all_fds`` is true, all file descriptors registered on the
        IOLoop will be closed (not just the ones created by the
        `IOLoop` itself).

        Many applications will only use a single `IOLoop` that runs for the
        entire lifetime of the process.  In that case closing the `IOLoop`
        is not necessary since everything will be cleaned up when the
        process exits.  `IOLoop.close` is provided mainly for scenarios
        such as unit tests, which create and destroy a large number of
        ``IOLoops``.

        An `IOLoop` must be completely stopped before it can be closed.  This
        means that `IOLoop.stop()` must be called *and* `IOLoop.start()` must
        be allowed to return before attempting to call `IOLoop.close()`.
        Therefore the call to `close` will usually appear just after
        the call to `start` rather than near the call to `stop`.

        .. versionchanged:: 3.1
           If the `IOLoop` implementation supports non-integer objects
           for "file descriptors", those objects will have their
           ``close`` method when ``all_fds`` is true.
        NNotImplementedError)r"   all_fdsr   r   r   close  s    zIOLoop.closec                 C   s
   t  dS )a  Registers the given handler to receive the given events for ``fd``.

        The ``fd`` argument may either be an integer file descriptor or
        a file-like object with a ``fileno()`` method (and optionally a
        ``close()`` method, which may be called when the `IOLoop` is shut
        down).

        The ``events`` argument is a bitwise or of the constants
        ``IOLoop.READ``, ``IOLoop.WRITE``, and ``IOLoop.ERROR``.

        When an event occurs, ``handler(fd, events)`` will be run.

        .. versionchanged:: 4.0
           Added the ability to pass file-like objects in addition to
           raw file descriptors.
        Nr6   )r"   fdhandlereventsr   r   r   add_handler  s    zIOLoop.add_handlerc                 C   s
   t  dS )zChanges the events we listen for ``fd``.

        .. versionchanged:: 4.0
           Added the ability to pass file-like objects in addition to
           raw file descriptors.
        Nr6   )r"   r:   r<   r   r   r   update_handler1  s    zIOLoop.update_handlerc                 C   s
   t  dS )zStop listening for events on ``fd``.

        .. versionchanged:: 4.0
           Added the ability to pass file-like objects in addition to
           raw file descriptors.
        Nr6   r"   r:   r   r   r   remove_handler:  s    zIOLoop.remove_handlerc                 C   s
   t  dS )a  Sends a signal if the `IOLoop` is blocked for more than
        ``s`` seconds.

        Pass ``seconds=None`` to disable.  Requires Python 2.6 on a unixy
        platform.

        The action parameter is a Python signal handler.  Read the
        documentation for the `signal` module for more information.
        If ``action`` is None, the process will be killed if it is
        blocked for too long.
        Nr6   r"   secondsactionr   r   r   set_blocking_signal_thresholdC  s    z$IOLoop.set_blocking_signal_thresholdc                 C   s   |  || j dS )zLogs a stack trace if the `IOLoop` is blocked for more than
        ``s`` seconds.

        Equivalent to ``set_blocking_signal_threshold(seconds,
        self.log_stack)``
        N)rD   	log_stack)r"   rB   r   r   r   set_blocking_log_thresholdQ  s    z!IOLoop.set_blocking_log_thresholdc              	   C   s    t d| jdt| dS )z|Signal handler to log the stack trace of the current thread.

        For use with `set_blocking_signal_threshold`.
        z#IOLoop blocked for %f seconds in
%s N)r
   Zwarning_blocking_signal_thresholdjoin	tracebackformat_stack)r"   signalframer   r   r   rE   Z  s    zIOLoop.log_stackc                 C   s
   t  dS )zStarts the I/O loop.

        The loop will run until one of the callbacks calls `stop()`, which
        will make the loop stop after the current event iteration completes.
        Nr6   r!   r   r   r   startc  s    zIOLoop.startc                 C   s0   t t jtdjtdjgs,t  dS )a  The IOLoop catches and logs exceptions, so it's
        important that log output be visible.  However, python's
        default behavior for non-root loggers (prior to python
        3.2) is to print an unhelpful "no handlers could be
        found" message rather than the actual log entry, so we
        must explicitly configure logging if we've made it this
        far without anything.

        This method should be called from start() in subclasses.
        tornadoztornado.applicationN)anyloggingZ	getLoggerhandlersZbasicConfigr!   r   r   r   _setup_loggingk  s
    


zIOLoop._setup_loggingc                 C   s
   t  dS )aA  Stop the I/O loop.

        If the event loop is not currently running, the next call to `start()`
        will return immediately.

        To use asynchronous methods from otherwise-synchronous code (such as
        unit tests), you can start and stop the event loop like this::

          ioloop = IOLoop()
          async_method(ioloop=ioloop, callback=ioloop.stop)
          ioloop.start()

        ``ioloop.start()`` will return after ``async_method`` has run
        its callback, whether that callback was invoked before or
        after ``ioloop.start``.

        Note that even after `stop` has been called, the `IOLoop` is not
        completely stopped until `IOLoop.start` has also returned.
        Some work that was scheduled before the call to `stop` may still
        be run before the `IOLoop` shuts down.
        Nr6   r!   r   r   r   stop{  s    zIOLoop.stopc                    s|   dg fdd} | |dur> | j}  |durX| d  sptd| d  S )a  Starts the `IOLoop`, runs the given function, and stops the loop.

        The function must return either a yieldable object or
        ``None``. If the function returns a yieldable object, the
        `IOLoop` will run until the yieldable is resolved (and
        `run_sync()` will return the yieldable's result). If it raises
        an exception, the `IOLoop` will stop and the exception will be
        re-raised to the caller.

        The keyword-only argument ``timeout`` may be used to set
        a maximum duration for the function.  If the timeout expires,
        a `TimeoutError` is raised.

        This method is useful in conjunction with `tornado.gen.coroutine`
        to allow asynchronous calls in a ``main()`` function::

            @gen.coroutine
            def main():
                # do stuff...

            if __name__ == '__main__':
                IOLoop.current().run_sync(main)

        .. versionchanged:: 4.3
           Returning a non-``None``, non-yieldable value is now an error.
        Nc                     s   z&  } | d ur$ddl m} || } W n. tyT   t d< d t  Y n,0 t| rh| d< nt d< d |  	d fdd d S )Nr   )convert_yieldedc                    s      S r*   )rT   futurer!   r   r   <lambda>      z.IOLoop.run_sync.<locals>.run.<locals>.<lambda>)
Ztornado.genrU   	Exceptionr   Zset_exc_infosysexc_infor   Z
set_result
add_future)resultrU   funcZfuture_cellr"   r   r   run  s    


zIOLoop.run_sync.<locals>.runr   z$Operation timed out after %s seconds)	add_callbackadd_timeouttimerT   rN   remove_timeoutZdoner   r^   )r"   r`   timeoutra   Ztimeout_handler   r_   r   run_sync  s    

zIOLoop.run_syncc                 C   s   t   S )a  Returns the current time according to the `IOLoop`'s clock.

        The return value is a floating-point number relative to an
        unspecified time in the past.

        By default, the `IOLoop`'s time function is `time.time`.  However,
        it may be configured to use e.g. `time.monotonic` instead.
        Calls to `add_timeout` that pass a number instead of a
        `datetime.timedelta` should use this function to compute the
        appropriate time, so they can work no matter what time function
        is chosen.
        )rd   r!   r   r   r   rd     s    zIOLoop.timec                 O   sh   t |tjr&| j||g|R i |S t |tjrX| j|  t| |g|R i |S td| dS )a  Runs the ``callback`` at the time ``deadline`` from the I/O loop.

        Returns an opaque handle that may be passed to
        `remove_timeout` to cancel.

        ``deadline`` may be a number denoting a time (on the same
        scale as `IOLoop.time`, normally `time.time`), or a
        `datetime.timedelta` object for a deadline relative to the
        current time.  Since Tornado 4.0, `call_later` is a more
        convenient alternative for the relative case since it does not
        require a timedelta object.

        Note that it is not safe to call `add_timeout` from other threads.
        Instead, you must use `add_callback` to transfer control to the
        `IOLoop`'s thread, and then call `add_timeout` from there.

        Subclasses of IOLoop must implement either `add_timeout` or
        `call_at`; the default implementations of each will call
        the other.  `call_at` is usually easier to implement, but
        subclasses that wish to maintain compatibility with Tornado
        versions prior to 4.0 must use `add_timeout` instead.

        .. versionchanged:: 4.0
           Now passes through ``*args`` and ``**kwargs`` to the callback.
        Unsupported deadline %rN)	
isinstancenumbersRealcall_atdatetimeZ	timedeltard   r   	TypeError)r"   deadlinecallbackargskwargsr   r   r   rc     s    zIOLoop.add_timeoutc                 O   s"   | j |  | |g|R i |S )a  Runs the ``callback`` after ``delay`` seconds have passed.

        Returns an opaque handle that may be passed to `remove_timeout`
        to cancel.  Note that unlike the `asyncio` method of the same
        name, the returned object does not have a ``cancel()`` method.

        See `add_timeout` for comments on thread-safety and subclassing.

        .. versionadded:: 4.0
        )rl   rd   )r"   Zdelayrp   rq   rr   r   r   r   
call_later  s    zIOLoop.call_laterc                 O   s   | j ||g|R i |S )a  Runs the ``callback`` at the absolute time designated by ``when``.

        ``when`` must be a number using the same reference point as
        `IOLoop.time`.

        Returns an opaque handle that may be passed to `remove_timeout`
        to cancel.  Note that unlike the `asyncio` method of the same
        name, the returned object does not have a ``cancel()`` method.

        See `add_timeout` for comments on thread-safety and subclassing.

        .. versionadded:: 4.0
        )rc   )r"   Zwhenrp   rq   rr   r   r   r   rl     s    zIOLoop.call_atc                 C   s
   t  dS )zCancels a pending timeout.

        The argument is a handle as returned by `add_timeout`.  It is
        safe to call `remove_timeout` even if the callback has already
        been run.
        Nr6   r"   rf   r   r   r   re     s    zIOLoop.remove_timeoutc                 O   s
   t  dS )a3  Calls the given callback on the next I/O loop iteration.

        It is safe to call this method from any thread at any time,
        except from a signal handler.  Note that this is the **only**
        method in `IOLoop` that makes this thread-safety guarantee; all
        other interaction with the `IOLoop` must be done from that
        `IOLoop`'s thread.  `add_callback()` may be used to transfer
        control from other threads to the `IOLoop`'s thread.

        To add a callback from a signal handler, see
        `add_callback_from_signal`.
        Nr6   r"   rp   rq   rr   r   r   r   rb   !  s    zIOLoop.add_callbackc                 O   s
   t  dS )aS  Calls the given callback on the next I/O loop iteration.

        Safe for use from a Python signal handler; should not be used
        otherwise.

        Callbacks added with this method will be run without any
        `.stack_context`, to avoid picking up the context of the function
        that was interrupted by the signal.
        Nr6   ru   r   r   r   add_callback_from_signal0  s    
zIOLoop.add_callback_from_signalc                 O   sD   t  ( | j|g|R i | W d   n1 s60    Y  dS )ag  Calls the given callback on the next IOLoop iteration.

        Unlike all other callback-related methods on IOLoop,
        ``spawn_callback`` does not associate the callback with its caller's
        ``stack_context``, so it is suitable for fire-and-forget callbacks
        that should not interfere with the caller.

        .. versionadded:: 4.0
        Nr   ZNullContextrb   ru   r   r   r   spawn_callback<  s    

zIOLoop.spawn_callbackc                    s.   t |sJ t  | fdd dS )zSchedules a callback on the ``IOLoop`` when the given
        `.Future` is finished.

        The callback is invoked with one argument, the
        `.Future`.
        c                    s     | S r*   )rb   rV   rp   r"   r   r   rX   S  rY   z#IOLoop.add_future.<locals>.<lambda>N)r   r   wrapZadd_done_callback)r"   rW   rp   r   ry   r   r]   I  s
    
zIOLoop.add_futurec                 C   st   zR| }|durPddl m} z||}W n |jy>   Y n0 | |dd  W n tyn   | | Y n0 dS )zMRuns a callback with error handling.

        For use in subclasses.
        Nr   )genc                 S   s   |   S r*   )r^   )fr   r   r   rX   j  rY   z&IOLoop._run_callback.<locals>.<lambda>)rO   r{   rU   ZBadYieldErrorr]   rZ   handle_callback_exception)r"   rp   Zretr{   r   r   r   _run_callbackU  s    zIOLoop._run_callbackc                 C   s   t jd|dd dS )aU  This method is called whenever a callback run by the `IOLoop`
        throws an exception.

        By default simply logs the exception as an error.  Subclasses
        may override this method to customize reporting of exceptions.

        The exception itself is not passed explicitly, but is available
        in `sys.exc_info`.
        zException in callback %rTr\   N)r	   error)r"   rp   r   r   r   r}   n  s    
z IOLoop.handle_callback_exceptionc                 C   s.   z|  |fW S  ty(   ||f Y S 0 dS )a  Returns an (fd, obj) pair from an ``fd`` parameter.

        We accept both raw file descriptors and file-like objects as
        input to `add_handler` and related methods.  When a file-like
        object is passed, we must retain the object itself so we can
        close it correctly when the `IOLoop` shuts down, but the
        poller interfaces favor file descriptors (they will accept
        file-like objects and call ``fileno()`` for you, but they
        always return the descriptor itself).

        This method is provided for use by `IOLoop` subclasses and should
        not generally be used by application code.

        .. versionadded:: 4.0
        N)filenoAttributeErrorr?   r   r   r   split_fdz  s    zIOLoop.split_fdc                 C   sF   z.z|   W n ty*   t | Y n0 W n ty@   Y n0 dS )ak  Utility method to close an ``fd``.

        If ``fd`` is a file-like object, we close it directly; otherwise
        we use `os.close`.

        This method is provided for use by `IOLoop` subclasses (in
        implementations of ``IOLoop.close(all_fds=True)`` and should
        not generally be used by application code.

        .. versionadded:: 4.0
        N)r9   r   osOSErrorr?   r   r   r   close_fd  s    zIOLoop.close_fd)T)N)F)N)9r   r   r   __doc__Z_EPOLLINZ	_EPOLLPRIZ	_EPOLLOUTZ	_EPOLLERRZ	_EPOLLHUPZ_EPOLLRDHUPZ_EPOLLONESHOTZ_EPOLLETZNONEREADZWRITEERROR	threadingLockr   Zlocalr&   staticmethodr   r    r#   r$   r'   r)   r+   classmethodr-   r3   r5   r9   r=   r>   r@   rD   rF   rE   rN   rS   rT   rg   rd   rc   rs   rl   re   rb   rv   rx   r]   r~   r}   r   r   r   r   r   r   r   G   sp   8







	
				
7"	r   c                       s   e Zd ZdZd fdd	ZdddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Z  ZS ) 
PollIOLoopa  Base class for IOLoops built around a select-like function.

    For concrete implementations, see `tornado.platform.epoll.EPollIOLoop`
    (Linux), `tornado.platform.kqueue.KQueueIOLoop` (BSD and Mac), or
    `tornado.platform.select.SelectIOLoop` (all platforms).
    Nc                    s   t t jf i | | _t jdr6t j  |p>tj _i  _	i  _
g  _t  _g  _d _d _d _d _d  _d  _t  _t  _  j  fdd j d S )Nr   r   Fc                    s
    j  S r*   )_wakerZconsume)r:   r<   r!   r   r   rX     rY   z'PollIOLoop.initialize.<locals>.<lambda>)superr   r5   _implr   r   r   rd   	time_func	_handlers_events
_callbacksr   r   _callback_lock	_timeouts_cancellations_running_stopped_closing_thread_identrH   	itertoolscount_timeout_counterr   r   r=   r   )r"   implr   rr   	__class__r!   r   r5     s,    


zPollIOLoop.initializeFc                 C   s   | j  d| _W d    n1 s"0    Y  | | j  |r^| j D ]\}}| | qJ| j  | j	  d | _
d | _d S )NT)r   r   r@   r   r   r   valuesr   r9   r   r   r   )r"   r8   r:   r;   r   r   r   r9     s    $

zPollIOLoop.closec                 C   s:   |  |\}}|t|f| j|< | j||| jB  d S r*   )r   r   rz   r   r   registerr   )r"   r:   r;   r<   objr   r   r   r=     s    zPollIOLoop.add_handlerc                 C   s&   |  |\}}| j||| jB  d S r*   )r   r   Zmodifyr   )r"   r:   r<   r   r   r   r   r>     s    zPollIOLoop.update_handlerc                 C   s`   |  |\}}| j|d  | j|d  z| j| W n  tyZ   tjddd Y n0 d S )NzError deleting fd from IOLoopTr   )	r   r   popr   r   Z
unregisterrZ   r
   debug)r"   r:   r   r   r   r   r@     s    zPollIOLoop.remove_handlerc                 C   sF   t tdstd d S || _|d urBttj|d ur:|ntj d S )N	setitimerzPset_blocking_signal_threshold requires a signal module with the setitimer method)r   rL   r
   r   rH   SIGALRMSIG_DFLrA   r   r   r   rD     s    

z(PollIOLoop.set_blocking_signal_thresholdc                 C   s  | j rtd|   | jr&d| _d S ttjdd }| tj_t	 | _
d| _ d }ttdrtjdkrz*t| j }|dkrt| d }W n ty   d }Y n0 z| j | j}g | _W d    n1 s0    Y  g }| jr|  }| jrR| jd jd u r&t| j |  jd	8  _q| jd j|krR|t| j qqRq| jd
kr| jt| jd	? krd| _dd | jD | _t| j |D ]}| | q|D ]}|jd ur| |j qd  } } }}| jrd}n2| jr| jd j|   }t dt!|t"}nt"}| j s.q| j#d urJt$tj%dd z| j&'|}	W nF t(y }
 z,t)|
t*j+krW Y d }
~
qn W Y d }
~
n
d }
~
0 0 | j#d urt$tj%| j#d | j,-|	 | j,rv| j,. \}}z| j/| \}}||| W nr t0t1fyL }
 z0t)|
t*j2kr&n| 3| j/4| W Y d }
~
n.d }
~
0  t(yp   | 3| j/4| Y n0 qd  }}qW d| _| j#d urt$tj%dd |tj_|d urt| n@d| _| j#d urt$tj%dd |tj_|d ur t| 0 d S )NzIOLoop is already runningFr   Tset_wakeup_fdposixr   r   i   c                 S   s   g | ]}|j d ur|qS r*   )rp   ).0xr   r   r   
<listcomp>9  s   
z$PollIOLoop.start.<locals>.<listcomp>g        )5r   r4   rS   r   r%   r   r&   r   thread	get_identr   r   rL   r   namer   r   Zwrite_fileno
ValueErrorr   r   r   rd   rp   heapqheappopr   ro   appendlenheapifyr~   maxmin_POLL_TIMEOUTrH   r   ITIMER_REALr   ZpollrZ   r   errnoZEINTRr   updatepopitemr   r   IOErrorZEPIPEr}   get)r"   Zold_currentZold_wakeup_fdZ	callbacksZdue_timeoutsZnowrp   rf   Zpoll_timeoutZevent_pairser:   r<   Zfd_objZhandler_funcr   r   r   rN     s    


$&

zPollIOLoop.startc                 C   s   d| _ d| _| j  d S )NFT)r   r   r   waker!   r   r   r   rT     s    zPollIOLoop.stopc                 C   s   |   S r*   )r   r!   r   r   r   rd     s    zPollIOLoop.timec                 O   s8   t |tjt|g|R i || }t| j| |S r*   )_Timeout	functoolspartialr   rz   r   heappushr   )r"   ro   rp   rq   rr   rf   r   r   r   rl     s    zPollIOLoop.call_atc                 C   s   d |_ |  jd7  _d S )Nr   )rp   r   rt   r   r   r   re     s    zPollIOLoop.remove_timeoutc                 O   s   t  | jkr| jb | jr,W d    d S | j }| jtjt	
|g|R i | |rh| j  W d    q1 s|0    Y  n0| jrd S | jtjt	
|g|R i | d S r*   )r   r   r   r   r   r   r   r   r   r   rz   r   r   )r"   rp   rq   rr   Z
list_emptyr   r   r   rb     s.    
*

zPollIOLoop.add_callbackc                 O   sD   t  ( | j|g|R i | W d    n1 s60    Y  d S r*   rw   ru   r   r   r   rv     s    
z#PollIOLoop.add_callback_from_signal)N)F)r   r   r   r   r5   r9   r=   r>   r@   rD   rN   rT   rd   rl   re   rb   rv   __classcell__r   r   r   r   r     s   
	
 	 r   c                   @   s0   e Zd ZdZg dZdd Zdd Zdd Zd	S )
r   z2An IOLoop timeout, a UNIX timestamp and a callback)ro   rp   
tiebreakerc                 C   s4   t |tjstd| || _|| _t|j| _d S )Nrh   )	ri   rj   rk   rn   ro   rp   nextr   r   )r"   ro   rp   io_loopr   r   r   __init__  s
    z_Timeout.__init__c                 C   s   | j | jf|j |jfk S r*   ro   r   r"   otherr   r   r   __lt__  s    

z_Timeout.__lt__c                 C   s   | j | jf|j |jfkS r*   r   r   r   r   r   __le__  s    

z_Timeout.__le__N)r   r   r   r   	__slots__r   r   r   r   r   r   r   r     s
   r   c                   @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )PeriodicCallbacka  Schedules the given callback to be called periodically.

    The callback is called every ``callback_time`` milliseconds.
    Note that the timeout is given in milliseconds, while most other
    time-related functions in Tornado use seconds.

    If the callback runs for longer than ``callback_time`` milliseconds,
    subsequent invocations will be skipped to get back on schedule.

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

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    Nc                 C   s:   || _ |dkrtd|| _|p&t | _d| _d | _d S )Nr   z4Periodic callback must have a positive callback_timeF)rp   r   callback_timer   r'   r   r   _timeout)r"   rp   r   r   r   r   r   r     s    zPeriodicCallback.__init__c                 C   s   d| _ | j | _|   dS )zStarts the timer.TN)r   r   rd   _next_timeout_schedule_nextr!   r   r   r   rN     s    zPeriodicCallback.startc                 C   s(   d| _ | jdur$| j| j d| _dS )zStops the timer.FN)r   r   r   re   r!   r   r   r   rT      s    
zPeriodicCallback.stopc                 C   s   | j S )zaReturn True if this `.PeriodicCallback` has been started.

        .. versionadded:: 4.1
        )r   r!   r   r   r   
is_running  s    zPeriodicCallback.is_runningc                 C   s\   | j s
d S zBz|  W W |   S  ty@   | j| j Y n0 W |   n
|   0 d S r*   )r   rp   r   rZ   r   r}   r!   r   r   r   _run  s    
zPeriodicCallback._runc                 C   sb   | j r^| j }| j|krJ| jd }|  jt|| j | d | 7  _| j| j| j| _	d S )Ng     @@r   )
r   r   rd   r   r   mathZfloorrc   r   r   )r"   Zcurrent_timeZcallback_time_secr   r   r   r     s    


&zPeriodicCallback._schedule_next)N)
r   r   r   r   r   rN   rT   r   r   r   r   r   r   r   r     s   
	
r   )/r   Z
__future__r   r   r   r   rm   r   r   r   r   rQ   rj   r   r2   r[   r   rd   rJ   r   Z
concurrentr   r   logr	   r
   rG   r   utilr   r   r   rL   ImportErrorr   _threadZplatform.autor   r   r   rZ   r   r   r   objectr   r   r   r   r   r   <module>   sP   
    a  &