B
    h/                 @   s   d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlZddlZddlmZmZmZmZmZmZ ejrddlmZmZmZ edZG d	d
 d
e	ZG dd deZG dd deZejejdddZejejdddZG dd dejZdS )a  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.
    N)	get_ident)convert_yielded)IOLoop_Selectable)AnyTypeVar	AwaitableCallableUnionOptional)SetDictTuple_Tc                   sH  e Zd Zejedd fddZd,eddddZe	e
ef ed	 e
dd
ddZe	e
ef e
ddddZe	e
ef ddddZe
e
ddddZddddZddddZeed	 eeedddZeddddZeeedd d!d"Zeeedd d#d$Zeejj ed%ef eee d&d'd(Zejjdd)d*d+Z   Z!S )-BaseAsyncIOLoopN)asyncio_loopkwargsreturnc                s   | _ i  _t  _t  _d _x"ttjD ]}|	 r.tj|= q.W  tj|< d _
tt jf | d d fdd} | d S )NFr   )r   c                  s   t   _d S )N)r   _thread_identity )selfr   UC:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_packages\tornado\platform\asyncio.pyassign_thread_identityG   s    z:BaseAsyncIOLoop.initialize.<locals>.assign_thread_identity)r   handlerssetreaderswritersclosinglistr   _ioloop_for_asyncio	is_closedr   superr   
initializeadd_callback)r   r   r   loopr   )	__class__)r   r   r"   )   s    
zBaseAsyncIOLoop.initializeF)all_fdsr   c             C   sX   d| _ x8t| jD ]*}| j| \}}| | |r| | qW tj| j= | j  d S )NT)	r   r   r   remove_handlerclose_fdr   r   r   close)r   r&   fdfileobjhandler_funcr   r   r   r)   L   s    

zBaseAsyncIOLoop.close).N)r*   handlereventsr   c             C   s   |  |\}}|| jkr$td| ||f| j|< |tj@ r^| j|| j|tj | j	| |tj
@ r| j|| j|tj
 | j	| d S )Nzfd %s added twice)split_fdr   
ValueErrorr   READr   
add_reader_handle_eventsr   addWRITE
add_writerr   )r   r*   r-   r.   r+   r   r   r   add_handler[   s    


zBaseAsyncIOLoop.add_handler)r*   r.   r   c             C   s   |  |\}}|tj@ rF|| jkrh| j|| j|tj | j| n"|| jkrh| j| | j	| |tj
@ r|| jkr| j|| j|tj
 | j| n"|| jkr| j| | j	| d S )N)r/   r   r1   r   r   r2   r3   r4   remove_readerremover5   r   r6   remove_writer)r   r*   r.   r+   r   r   r   update_handleri   s    





zBaseAsyncIOLoop.update_handler)r*   r   c             C   sl   |  |\}}|| jkrd S || jkr>| j| | j| || jkr`| j| | j| | j|= d S )N)r/   r   r   r   r8   r9   r   r:   )r   r*   r+   r   r   r   r'   |   s    


zBaseAsyncIOLoop.remove_handlerc             C   s   | j | \}}||| d S )N)r   )r   r*   r.   r+   r,   r   r   r   r3      s    zBaseAsyncIOLoop._handle_events)r   c          	   C   s^   yt  }W n ttfk
r(   d }Y nX z"|   t | j | j  W d t | X d S )N)asyncioget_event_loopRuntimeErrorAssertionError_setup_loggingset_event_loopr   run_forever)r   Zold_loopr   r   r   start   s    
zBaseAsyncIOLoop.startc             C   s   | j   d S )N)r   stop)r   r   r   r   rD      s    zBaseAsyncIOLoop.stop)whencallbackargsr   r   c             O   s.   | j td||   | jtj|f||S )Nr   )r   
call_latermaxtime_run_callback	functoolspartial)r   rE   rF   rG   r   r   r   r   call_at   s    zBaseAsyncIOLoop.call_at)timeoutr   c             C   s   |   d S )N)cancel)r   rO   r   r   r   remove_timeout   s    zBaseAsyncIOLoop.remove_timeout)rF   rG   r   r   c             O   sV   t  | jkr| jj}n| jj}y|| jtj|f|| W n tk
rP   Y nX d S )N)	r   r   r   	call_sooncall_soon_threadsaferK   rL   rM   r>   )r   rF   rG   r   rR   r   r   r   r#      s    
zBaseAsyncIOLoop.add_callbackc             O   s<   y"| j | jtj|f|| W n tk
r6   Y nX d S )N)r   rS   rK   rL   rM   r>   )r   rF   rG   r   r   r   r   add_callback_from_signal   s
    z(BaseAsyncIOLoop.add_callback_from_signal.)executorfuncrG   r   c             G   s   | j j||f| S )N)r   run_in_executor)r   rU   rV   rG   r   r   r   rW      s    zBaseAsyncIOLoop.run_in_executor)rU   r   c             C   s   | j |S )N)r   set_default_executor)r   rU   r   r   r   rX      s    z$BaseAsyncIOLoop.set_default_executor)F)"__name__
__module____qualname__r<   AbstractEventLoopr   r"   boolr)   r
   intr   r	   r7   r;   r'   r3   rC   rD   floatobjectrN   rQ   r#   rT   r   
concurrentfuturesExecutorr   r   rW   rX   __classcell__r   r   )r%   r   r   (   s*   !



r   c                   s6   e Zd ZdZedd fddZddddZ  ZS )	AsyncIOMainLoopa  ``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.
    N)r   r   c                s   t t| jt f| d S )N)r!   re   r"   r<   r=   )r   r   )r%   r   r   r"      s    zAsyncIOMainLoop.initialize)r   c             C   s   d S )Nr   )r   r   r   r   make_current   s    zAsyncIOMainLoop.make_current)rY   rZ   r[   __doc__r   r"   rf   rd   r   r   )r%   r   re      s   re   c                   sZ   e Zd ZdZedd fddZdedd fdd	Zdd
ddZdd
ddZ	  Z
S )AsyncIOLoopa  ``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.
    N)r   r   c                sJ   d| _ t }ytt| j|f| W n tk
rD   |   Y nX d S )NF)
is_currentr<   new_event_loopr!   rh   r"   	Exceptionr)   )r   r   r$   )r%   r   r   r"      s    zAsyncIOLoop.initializeF)r&   r   c                s$   | j r|   tt| j|d d S )N)r&   )ri   clear_currentr!   rh   r)   )r   r&   )r%   r   r   r)     s    zAsyncIOLoop.close)r   c          	   C   sJ   | j s:yt | _W n ttfk
r2   d | _Y nX d| _ t| j d S )NT)ri   r<   r=   old_asyncior>   r?   rA   r   )r   r   r   r   rf     s    zAsyncIOLoop.make_currentc             C   s   | j rt| j d| _ d S )NF)ri   r<   rA   rm   )r   r   r   r   _clear_current_hook  s    zAsyncIOLoop._clear_current_hook)F)rY   rZ   r[   rg   r   r"   r]   r)   rf   rn   rd   r   r   )r%   r   rh      s
   	rh   )asyncio_futurer   c             C   s   | S )zConvert 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.
    r   )ro   r   r   r   to_tornado_future  s    	rp   )tornado_futurer   c             C   s   t | S )as  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   )rq   r   r   r   to_asyncio_future&  s    rr   c                   s(   e Zd ZdZejd fddZ  ZS )AnyThreadEventLoopPolicyaD  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

    )r   c          	      s:   y
t   S  ttfk
r4   |  }| | |S X d S )N)r!   r=   r>   r?   rj   rA   )r   r$   )r%   r   r   r=   H  s    

z'AnyThreadEventLoopPolicy.get_event_loop)rY   rZ   r[   rg   r<   r\   r=   rd   r   r   )r%   r   rs   6  s   rs   ) rg   concurrent.futuresra   rL   	threadingr   tornado.genr   tornado.ioloopr   r   r<   typingr   r   r   r	   r
   r   TYPE_CHECKINGr   r   r   r   r   re   rh   Futurerp   rr   DefaultEventLoopPolicyrs   r   r   r   r   <module>   s$     (3