U
    Ú²g‰  ã                   @   s¢   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m	Z	m
Z
mZ ejdkr\d dlmZ nd dlmZ e
dƒZedƒZe
dƒZG d	d
„ d
ƒZG dd„ deƒZdS )é    N)ÚExecutorÚFuture)ÚTYPE_CHECKINGÚAnyÚCallableÚTypeVarÚUnion)é   é
   )Ú	ParamSpecÚ_TÚ_PÚ_Rc                   @   s>   e Zd ZdZdeeef ejejdœdd„Z	ddœdd	„Z
dS )
Ú	_WorkItemz¡
    Represents an item needing to be run in the executor.
    Copied from ThreadPoolExecutor (but it's private, so we're not going to rely on importing it)
    ú
Future[_R]©ÚfutureÚfnÚargsÚkwargsc                 O   s   || _ || _|| _|| _d S ©Nr   )Úselfr   r   r   r   © r   úC/tmp/pip-unpacked-wheel-hay1n6u8/asgiref/current_thread_executor.pyÚ__init__   s    z_WorkItem.__init__N©Úreturnc              
   C   sj   d}| j  ¡ sd S z| j| j| jŽ}W n2 tk
rX } z| j  |¡ d } W 5 d }~X Y nX | j  |¡ d S )NT)r   Zset_running_or_notify_cancelr   r   r   ÚBaseExceptionZset_exceptionZ
set_result)r   Z__traceback_hide__ÚresultÚexcr   r   r   Úrun#   s    
z_WorkItem.run)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r    r   r   r   r   r      s   
ûr   c                   @   sZ   e Zd ZdZddœdd„Zdddœdd	„Zeeef ej	ej
d
dœdd„ZesVdd„ ZdS )ÚCurrentThreadExecutorz¾
    An Executor that actually runs code in the thread it is instantiated in.
    Passed to other threads running async code, so they can run sync code in
    the thread they came from.
    Nr   c                 C   s   t  ¡ | _t ¡ | _d| _d S )NF)Ú	threadingÚcurrent_threadÚ_work_threadÚqueueÚQueueÚ_work_queueÚ_broken)r   r   r   r   r   8   s    

zCurrentThreadExecutor.__init__zFuture[Any])r   r   c                 C   sj   t  ¡ | jkrtdƒ‚| | jj¡ z8| j ¡ }||kr@W ¢"dS t	|t
ƒsNt‚| ¡  ~q&W 5 d| _X dS )z¤
        Runs the code in the work queue until a result is available from the future.
        Should be run from the thread the executor is initialised in.
        z<You cannot run CurrentThreadExecutor from a different threadTN)r&   r'   r(   ÚRuntimeErrorZadd_done_callbackr+   Úputr,   ÚgetÚ
isinstancer   ÚAssertionErrorr    )r   r   Ú	work_itemr   r   r   Úrun_until_future=   s    ÿ
z&CurrentThreadExecutor.run_until_futurer   )r   r   r   r   c                 O   sL   t  ¡ | jkrtdƒ‚| jr$tdƒ‚tƒ }t||f|ž|Ž}| j |¡ |S )Nz@You cannot submit onto CurrentThreadExecutor from its own threadz/CurrentThreadExecutor already quit or is broken)	r&   r'   r(   r-   r,   r   r   r+   r.   )r   r   r   r   Úfr2   r   r   r   Ú_submitV   s    ÿzCurrentThreadExecutor._submitc                 O   s   | j |f|ž|ŽS r   )r5   )r   r   r   r   r   r   r   Úsubmitr   s    zCurrentThreadExecutor.submit)r!   r"   r#   r$   r   r3   r   r   r   r   r   r5   r   r6   r   r   r   r   r%   1   s   
ûr%   )r)   Úsysr&   Úconcurrent.futuresr   r   Útypingr   r   r   r   r   Úversion_infor   Ztyping_extensionsr   r   r   r   r%   r   r   r   r   Ú<module>   s   
 