a
    wh=                     @   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rXd 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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   Y/var/www/battlenet.uz/venv/lib/python3.9/site-packages/asgiref/current_thread_executor.py__init__   s    z_WorkItem.__init__N)returnc              
   C   sp   d}| j  sd S z| j| ji | j}W n4 ty^ } z| j | d } W Y d }~nd }~0 0 | 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                   @   sP   e Zd ZdZdddddZdddd	d
Zeeef ej	ej
dd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.
    zCurrentThreadExecutor | NoneN)old_executorr   c                 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__Future[Any]r   r   c                    s   t   jkrtdddd fdd}||  jF  jsT jsT j  q< jshW d   q j	 }W d   n1 s0    Y  |
  ~q4d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 threadr.   Nr/   c                    s:    j   d _ j   W d    n1 s,0    Y  d S r   )r*   r,   notify)r   r   r   r   doneJ   s    z4CurrentThreadExecutor.run_until_future.<locals>.done)r%   r&   r'   RuntimeErrorZadd_done_callbackr*   r+   r,   waitpopleftr   )r   r   r2   	work_itemr   r1   r   run_until_future?   s    
(z&CurrentThreadExecutor.run_until_futurer   )r   r   r   r   c                O   s   t  | jkrtdt }t||g|R i |}| }|j: |jsj|j	| |j
  W d    qW d    n1 s~0    Y  |jd u rtd|j}q8|S )Nz@You cannot submit onto CurrentThreadExecutor from its own threadz/CurrentThreadExecutor already quit or is broken)r%   r&   r'   r3   r   r   r*   r,   r+   appendr0   r-   )r   r   r   r   fr6   executorr   r   r   submit]   s     
,
zCurrentThreadExecutor.submit)r   r    r!   r"   r   r7   r   r   r   r   r   r;   r   r   r   r   r#   1   s    
r#   )sysr%   collectionsr   concurrent.futuresr   r   typingr   r   r   version_infor
   Ztyping_extensionsr   r   r   r   r#   r   r   r   r   <module>   s   
 