
v^2                 @   s   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
 d d l m Z y d  d l Z Wn e k
 r d Z Yn XGd d   d  Z Gd d	   d	  Z e Z e Z d S)
    N)FutureThreadPoolExecutor   )CurrentThreadExecutor)Localc               @   sd   e  Z d  Z d Z i  Z e   Z d d d  Z d d   Z d d   Z	 d	 d
   Z
 d d   Z d S)AsyncToSynca  
    Utility class which turns an awaitable that only works on the thread with
    the event loop into a synchronous callable that works in a subthread.

    If the call stack contains an async loop, the code runs there.
    Otherwise, the code runs in a new loop in a new thread.

    Either way, this thread then pauses and waits to run any thread_sensitive
    code called from further down the call stack using SyncToAsync, before
    finally exiting once the async task returns.
    Fc             C   s   | |  _  y |  j  j |  _ Wn t k
 r0 Yn X| rC d  |  _ n@ y t j   |  _ Wn* t k
 r t t j	 d d   |  _ Yn Xd  S)Nmain_event_loop)
	awaitable__self__AttributeErrorr   asyncioget_event_loopRuntimeErrorgetattrSyncToAsyncthreadlocal)selfr	   Zforce_new_loop r   //tmp/pip-build-8lau8j11/asgiref/asgiref/sync.py__init__&   s    	zAsyncToSync.__init__c             O   s  y t  j   } Wn t k
 r$ Yn X| j   r= t d   t   } t j   } t |  j d  rs |  j j	 } n d  } t
   } | |  j _	 z |  j o |  j j   st  j   } t d d  }	 |	 j |  j | |  j | | | | t j     }
 | r
| j |
  |
 j   nG |  j j |  j j |  j | | | | t j     | r^| j |  Wd  t |  j d  r}|  j `	 | r| |  j _	 X| j   S)NznYou cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.currentmax_workersr   )r   r   r   Z
is_runningr   	threadingcurrent_threadhasattr	executorsr   r   r   Znew_event_loopr   submit_run_event_loop	main_wrapsysexc_infoZrun_until_futureresultZcall_soon_threadsafeZcreate_task)r   argskwargsZ
event_loopcall_resultsource_threadZold_current_executorZcurrent_executorloopZloop_executorZloop_futurer   r   r   __call__9   sJ    						zAsyncToSync.__call__c             C   s+  t  j |  z | j |  Wd z t j d k rE t  j |  } n t  j j |  } x | D] } | j   q^ W| j t  j | d d   xT | D]L } | j	   r q | j
   d k	 r | j d d d	 | j
   d
 | i  q Wt | d  r| j | j    Wd | j   t  j |  j  XXd S)zP
        Runs the given event loop (designed to be called in a thread).
        N      r   Zreturn_exceptionsTmessagez(unhandled exception during loop shutdown	exceptiontaskshutdown_asyncgens)r(   r)   r   )r   Zset_event_loopZrun_until_completer   version_infoZ	all_tasksTaskcancelZgather	cancelledr+   Zcall_exception_handlerr   r-   closer   )r   r&   coroZtasksr,   r   r   r   r   z   s,    
zAsyncToSync._run_event_loopc             C   s(   t  j |  j |  } t  j | |  j  S)z*
        Include self for methods
        )	functoolspartialr'   update_wrapperr	   )r   parentobjtypefuncr   r   r   __get__   s    zAsyncToSync.__get__c       	         s   t  j   } | |  j | <z yX | d r\ y | d  Wqs |  j | |   Id H} Yqs Xn |  j | |   Id H} Wn2 t k
 r } z | j |  WYd d } ~ Xn X| j |  Wd |  j | =Xd S)zs
        Wraps the awaitable with something that puts the result into the
        result/exception future.
        r   N)r   get_current_task
launch_mapr	   	Exceptionset_exception
set_result)	r   r"   r#   r$   r%   r    current_taskr!   er   r   r   r      s    
 zAsyncToSync.main_wrapN)__name__
__module____qualname____doc__r<   r   r   r   r'   r   r:   r   r   r   r   r   r      s   	A#r   c               @   s   e  Z d  Z d Z d e j k rP e j   Z e j	 e
 d e e j d    i  Z e j   Z e
 d d  Z d d d  Z d d	   Z d
 d   Z d d   Z e d d    Z d S)r   aK  
    Utility class which turns a synchronous callable into an awaitable that
    runs in a threadpool. It also sets a threadlocal inside the thread so
    calls to AsyncToSync can escape it.

    If thread_sensitive is passed, the code will run in the same thread as any
    outer code. This is needed for underlying Python code that is not
    threadsafe (for example, code which handles SQLite database connections).

    If the outermost program is async (i.e. SyncToAsync is outermost), then
    this will be a dedicated single sub-thread that all sync code runs in,
    one after the other. If the outermost program is sync (i.e. AsyncToSync is
    outermost), this will just be the main thread. This is achieved by idling
    with a CurrentThreadExecutor while AsyncToSync is blocking its sync parent,
    rather than just blocking.
    ZASGI_THREADSr   r   Fc             C   sZ   | |  _  t j |  |  | |  _ t j j |  _ y | j |  _ Wn t k
 rU Yn Xd  S)N)	r9   r4   r6   _thread_sensitiver   Z
coroutinesZ_is_coroutiner
   r   )r   r9   Zthread_sensitiver   r   r   r      s    		zSyncToAsync.__init__c                ss  t  j   } |  j rB t t j d  r6 t j j } qH |  j } n d  } t d  k	 r t j	   } t
 j |  j | |  } | j } | f } i  } n	 |  j } | j | t
 j |  j | |  j   t j   | | |   } t  j | d d  Id  H}	 t d  k	 roxn | D]f }
 y5 |
 j   | j |
  k r?|
 j | j |
   Wqt k
 rj|
 j | j |
   YqXqW|	 S)Nr   timeout)r   r   rF   r   r   r   r   single_thread_executorcontextvarsZcopy_contextr4   r5   r9   runZrun_in_executorthread_handlerr;   r   r    wait_forgetsetLookupError)r   r"   r#   r&   executorcontextchildr9   futureretZcvarr   r   r   r'      s@    							zSyncToAsync.__call__c             C   s   t  j |  j |  S)z*
        Include self for methods
        )r4   r5   r'   )r   r7   r8   r   r   r   r:     s    zSyncToAsync.__get__c       	      O   s   | |  j  _ t j   } t j j |  | k r9 d } n | |  j | <d } zD | d r y | d  Wq | | |   SYq Xn | | |   SWd | r |  j | =Xd S)zE
        Wraps the sync application with exception handling.
        FTr   N)r   r   r   r   r   r<   rM   )	r   r&   Zsource_taskr    r9   r"   r#   r   Z
parent_setr   r   r   rK   !  s    	
zSyncToAsync.thread_handlerc               C   sG   y* t  t d  r t j   St j j   SWn t k
 rB d SYn Xd S)zs
        Cross-version implementation of asyncio.current_task()

        Returns None if there is no task.
        r@   N)r   r   r@   r/   r   r   r   r   r   r;   A  s    
zSyncToAsync.get_current_taskN)rB   rC   rD   rE   osenvironr   r   r&   Zset_default_executorr   intr<   r   localr   rH   r   r'   r:   rK   staticmethodr;   r   r   r   r   r      s   
2 r   )r   Zasyncio.coroutinesr4   rU   r   r   concurrent.futuresr   r   Zcurrent_thread_executorr   rX   r   rI   ImportErrorr   r   Zsync_to_asyncZasync_to_syncr   r   r   r   <module>   s    