o
    ýÞh=  ã                   @   sž   d dl Z d dlZd dlmZ d dlmZmZ d dl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)Údeque)ÚExecutorÚFuture)ÚAnyÚCallableÚTypeVar)é   é
   )Ú	ParamSpecÚ_TÚ_PÚ_Rc                   @   s@   e Zd ZdZdddeeef dejdejfdd„Z	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ú
Future[_R]ÚfnÚargsÚkwargsc                 O   s   || _ || _|| _|| _d S )N)r   r   r   r   )Úselfr   r   r   r   © r   ú_/var/www/html/premium_crap/venv/lib/python3.10/site-packages/asgiref/current_thread_executor.pyÚ__init__   s   
z_WorkItem.__init__ÚreturnNc              
   C   sr   d}| j  ¡ s	d S z| j| ji | j¤Ž}W n ty0 } z| j  |¡ d } W Y d }~d S d }~ww | j  |¡ d S ©NT)r   Úset_running_or_notify_cancelr   r   r   ÚBaseExceptionÚset_exceptionÚ
set_result)r   Ú__traceback_hide__ÚresultÚexcr   r   r   Úrun#   s   
€ýz_WorkItem.run)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r!   r   r   r   r   r      s    þ
ýü
ûr   c                   @   sJ   e Zd ZdZddd„Zdd
d„Zdeeef dej	dej
ddfd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.
    Úold_executorúCurrentThreadExecutor | Noner   Nc                 C   s6   t  ¡ | _t  t  ¡ ¡| _tt ƒ | _d| _	|| _
d S )NF)Ú	threadingÚcurrent_threadÚ_work_threadÚ	ConditionÚLockÚ_work_readyr   r   Ú_work_itemsÚ_brokenÚ_old_executor)r   r'   r   r   r   r   8   s
   

zCurrentThreadExecutor.__init__r   úFuture[Any]c                    s¦   t  ¡ ˆ jkrtdƒ‚d	‡ fdd„}| |¡ 	 ˆ j�* ˆ js-ˆ js-ˆ j ¡  ˆ js-ˆ jr"ˆ js9	 W d  ƒ dS ˆ j 	¡ }W d  ƒ n1 sHw   Y  | 
¡  ~q)
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 threadr   r2   r   Nc                    s<   ˆ j � dˆ _ˆ j  ¡  W d   ƒ d S 1 sw   Y  d S r   )r.   r0   Únotify)r   ©r   r   r   ÚdoneJ   s   "þz4CurrentThreadExecutor.run_until_future.<locals>.doneT©r   r2   r   N)r)   r*   r+   ÚRuntimeErrorÚadd_done_callbackr.   r/   r0   ÚwaitÚpopleftr!   )r   r   r5   Ú	work_itemr   r4   r   Úrun_until_future?   s&   ÿ

ÿüú÷z&CurrentThreadExecutor.run_until_futurer   r   r   r   c                O   s¨   t  ¡ | jkrtdƒ‚tƒ }t||g|¢R i |¤Ž}| }	 |j� |js8|j 	|¡ |j 
¡  	 W d   ƒ |S W d   ƒ n1 sBw   Y  |jd u rPtdƒ‚|j}q)Nz@You cannot submit onto CurrentThreadExecutor from its own threadTz/CurrentThreadExecutor already quit or is broken)r)   r*   r+   r7   r   r   r.   r0   r/   Úappendr3   r1   )r   r   r   r   Úfr;   Úexecutorr   r   r   Úsubmit]   s*   ÿ
ûöÿ
÷zCurrentThreadExecutor.submit)r'   r(   r   Nr6   )r"   r#   r$   r%   r   r<   r   r   r   r   r   r@   r   r   r   r   r&   1   s    


þüûúr&   )Úsysr)   Úcollectionsr   Úconcurrent.futuresr   r   Útypingr   r   r   Úversion_infor
   Útyping_extensionsr   r   r   r   r&   r   r   r   r   Ú<module>   s    
 