σ
¨[c           @` sΫ   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l Z d e	 f d     YZ d	 e f d
     YZ d e f d     YZ d   Z d   Z d e j f d     YZ d S(   s   Bridges between the `asyncio` module and Tornado IOLoop.

.. versionadded:: 3.2

This module integrates Tornado with the ``asyncio`` module introduced
in Python 3.4. This makes it possible to combine the two libraries on
the same event loop.

.. deprecated:: 5.0

   While the code in this module is still used, it is now enabled
   automatically when `asyncio` is available, so applications should
   no longer need to refer to this module directly.

.. note::

   Tornado requires the `~asyncio.AbstractEventLoop.add_reader` family of
   methods, so it is not compatible with the `~asyncio.ProactorEventLoop` on
   Windows. Use the `~asyncio.SelectorEventLoop` instead.
i    (   t   absolute_importt   divisiont   print_functionN(   t   convert_yielded(   t   IOLoop(   t   stack_contextt   BaseAsyncIOLoopc           B` s   e  Z d    Z e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z e Z d   Z d   Z RS(   c         K` s   | |  _  i  |  _ t   |  _ t   |  _ t |  _ x0 t t j	  D] } | j
   rC t j	 | =qC qC W|  t j	 | <t t |   j |   d  S(   N(   t   asyncio_loopt   handlerst   sett   readerst   writerst   Falset   closingt   listR   t   _ioloop_for_asynciot	   is_closedt   superR   t
   initialize(   t   selfR   t   kwargst   loop(    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR   !   s    			c         C` sw   t  |  _ xM t |  j  D]< } |  j | \ } } |  j |  | r |  j |  q q Wt j |  j =|  j j	   d  S(   N(
   t   TrueR   R   R   t   remove_handlert   close_fdR   R   R   t   close(   R   t   all_fdst   fdt   fileobjt   handler_func(    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR   :   s    	c         C` sΥ   |  j  |  \ } } | |  j k r7 t d |   n  | t j |  f |  j | <| t j @r |  j j | |  j	 | t j  |  j
 j |  n  | t j @rΡ |  j j | |  j	 | t j  |  j j |  n  d  S(   Ns   fd %s added twice(   t   split_fdR   t
   ValueErrorR   t   wrapR   t   READR   t
   add_readert   _handle_eventsR
   t   addt   WRITEt
   add_writerR   (   R   R   t   handlert   eventsR   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   add_handlerI   s    		c         C` s  |  j  |  \ } } | t j @rf | |  j k r |  j j | |  j | t j  |  j j |  q n2 | |  j k r |  j j |  |  j j	 |  n  | t j
 @rι | |  j k r|  j j | |  j | t j
  |  j j |  qn2 | |  j k r|  j j |  |  j j	 |  n  d  S(   N(   R   R   R!   R
   R   R"   R#   R$   t   remove_readert   removeR%   R   R&   t   remove_writer(   R   R   R(   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   update_handlerW   s"    		c         C` s   |  j  |  \ } } | |  j k r( d  S| |  j k rZ |  j j |  |  j j |  n  | |  j k r |  j j |  |  j j |  n  |  j | =d  S(   N(   R   R   R
   R   R*   R+   R   R,   (   R   R   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR   l   s    c         C` s$   |  j  | \ } } | | |  d  S(   N(   R   (   R   R   R(   R   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR#   x   s    c         C` sp   y t  j   } Wn t t f k
 r/ d  } n Xz+ |  j   t  j |  j  |  j j   Wd  t  j |  Xd  S(   N(	   t   asynciot   get_event_loopt   RuntimeErrort   AssertionErrort   Nonet   _setup_loggingt   set_event_loopR   t   run_forever(   R   t   old_loop(    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   start|   s    

c         C` s   |  j  j   d  S(   N(   R   t   stop(   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR8      s    c         O` sD   |  j  j t d | |  j    |  j t j t j |  | |   S(   Ni    (	   R   t
   call_latert   maxt   timet   _run_callbackt	   functoolst   partialR   R    (   R   t   whent   callbackt   argsR   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   call_at   s    	c         C` s   | j    d  S(   N(   t   cancel(   R   t   timeout(    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   remove_timeout   s    c         O` sJ   y2 |  j  j |  j t j t j |  | |   Wn t k
 rE n Xd  S(   N(   R   t   call_soon_threadsafeR<   R=   R>   R   R    R0   (   R   R@   RA   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   add_callback   s    	#c         G` s   |  j  j | | |  S(   N(   R   t   run_in_executor(   R   t   executort   funcRA   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyRH   ₯   s    c         C` s   |  j  j |  S(   N(   R   t   set_default_executor(   R   RI   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyRK   ¨   s    (   t   __name__t
   __module__R   R   R   R)   R-   R   R#   R7   R8   RB   RE   RG   t   add_callback_from_signalRH   RK   (    (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR       s   											t   AsyncIOMainLoopc           B` s    e  Z d  Z d   Z d   Z RS(   s  ``AsyncIOMainLoop`` creates an `.IOLoop` that corresponds to the
    current ``asyncio`` event loop (i.e. the one returned by
    ``asyncio.get_event_loop()``).

    .. deprecated:: 5.0

       Now used automatically when appropriate; it is no longer necessary
       to refer to this class directly.

    .. versionchanged:: 5.0

       Closing an `AsyncIOMainLoop` now closes the underlying asyncio loop.
    c         K` s#   t  t |   j t j   |  d  S(   N(   R   RO   R   R.   R/   (   R   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR   Ί   s    c         C` s   d  S(   N(    (   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   make_current½   s    (   RL   RM   t   __doc__R   RP   (    (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyRO   ¬   s   	t   AsyncIOLoopc           B` s5   e  Z d  Z d   Z e d  Z d   Z d   Z RS(   s  ``AsyncIOLoop`` is an `.IOLoop` that runs on an ``asyncio`` event loop.
    This class follows the usual Tornado semantics for creating new
    ``IOLoops``; these loops are not necessarily related to the
    ``asyncio`` default event loop.

    Each ``AsyncIOLoop`` creates a new ``asyncio.EventLoop``; this object
    can be accessed with the ``asyncio_loop`` attribute.

    .. versionchanged:: 5.0

       When an ``AsyncIOLoop`` becomes the current `.IOLoop`, it also sets
       the current `asyncio` event loop.

    .. deprecated:: 5.0

       Now used automatically when appropriate; it is no longer necessary
       to refer to this class directly.
    c         K` sW   t  |  _ t j   } y t t |   j | |  Wn t k
 rR | j     n Xd  S(   N(	   R   t
   is_currentR.   t   new_event_loopR   RR   R   t	   ExceptionR   (   R   R   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR   Φ   s    	
c         C` s3   |  j  r |  j   n  t t |   j d |  d  S(   NR   (   RS   t   clear_currentR   RR   R   (   R   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR   α   s    	c         C` s_   |  j  sK y t j   |  _ Wn  t t f k
 r> d  |  _ n Xt |  _  n  t j |  j	  d  S(   N(
   RS   R.   R/   t   old_asyncioR0   R1   R2   R   R4   R   (   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyRP   ζ   s    	c         C` s)   |  j  r% t j |  j  t |  _  n  d  S(   N(   RS   R.   R4   RW   R   (   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   _clear_current_hookο   s    	(   RL   RM   RQ   R   R   R   RP   RX   (    (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyRR   Γ   s
   			c         C` s   |  S(   sί   Convert an `asyncio.Future` to a `tornado.concurrent.Future`.

    .. versionadded:: 4.1

    .. deprecated:: 5.0
       Tornado ``Futures`` have been merged with `asyncio.Future`,
       so this method is now a no-op.
    (    (   t   asyncio_future(    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   to_tornado_futureυ   s    	c         C` s
   t  |   S(   ss  Convert a Tornado yieldable object to an `asyncio.Future`.

    .. versionadded:: 4.1

    .. versionchanged:: 4.3
       Now accepts any yieldable object, not just
       `tornado.concurrent.Future`.

    .. deprecated:: 5.0
       Tornado ``Futures`` have been merged with `asyncio.Future`,
       so this method is now equivalent to `tornado.gen.convert_yielded`.
    (   R   (   t   tornado_future(    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   to_asyncio_future  s    t   AnyThreadEventLoopPolicyc           B` s   e  Z d  Z d   Z RS(   sD  Event loop policy that allows loop creation on any thread.

    The default `asyncio` event loop policy only automatically creates
    event loops in the main threads. Other threads must create event
    loops explicitly or `asyncio.get_event_loop` (and therefore
    `.IOLoop.current`) will fail. Installing this policy allows event
    loops to be created automatically on any thread, matching the
    behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).

    Usage::

        asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())

    .. versionadded:: 5.0

    c         C` sI   y t    j   SWn1 t t f k
 rD |  j   } |  j |  | SXd  S(   N(   R   R/   R0   R1   RT   R4   (   R   R   (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR/   "  s    (   RL   RM   RQ   R/   (    (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyR]     s   (   RQ   t
   __future__R    R   R   R=   t   tornado.genR   t   tornado.ioloopR   t   tornadoR   R.   R   RO   RR   RZ   R\   t   DefaultEventLoopPolicyR]   (    (    (    s7   lib/python2.7/site-packages/tornado/platform/asyncio.pyt   <module>   s   2		