o
    fC"                     @   s   d Z dZddlmZ ddlZddlZddlZddlZddlZddl	Z	e
 Zdae Zdd Zee ee	drEe	jejejejd	 G d
d deZdd ZG dd dejZG dd dejZdS )zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com)    )_baseNFc                  C   sf   t 
 daW d    n1 sw   Y  tt } | D ]	\}}|d  q| D ]\}}|  q(d S NT)_global_shutdown_lock	_shutdownlist_threads_queuesitemsputjoin)r   tq r   0/usr/lib/python3.10/concurrent/futures/thread.py_python_exit   s   
r   register_at_fork)beforeafter_in_childafter_in_parentc                   @   s&   e Zd Zdd Zdd ZeejZdS )	_WorkItemc                 C   s   || _ || _|| _|| _d S N)futurefnargskwargs)selfr   r   r   r   r   r   r   __init__/   s   
z_WorkItem.__init__c              
   C   sn   | j  sd S z| j| ji | j}W n ty. } z| j | d } W Y d }~d S d }~ww | j | d S r   )r   set_running_or_notify_cancelr   r   r   BaseExceptionset_exception
set_result)r   resultexcr   r   r   run5   s   
z_WorkItem.runN)	__name__
__module____qualname__r   r"   classmethodtypesGenericAlias__class_getitem__r   r   r   r   r   .   s    r   c                 C   s   |d ur(z||  W n t y'   tjjddd |  }|d ur$|  Y d S w z;	 |jdd}|d urG|  ~|  }|d urE|j  ~q)|  }t	sS|d u sS|j	rb|d urZd|_	|
d  W d S ~q* t yu   tjjddd Y d S w )NzException in initializer:T)exc_info)blockzException in worker)r   r   LOGGERcritical_initializer_failedgetr"   _idle_semaphorereleaser   r	   )executor_reference
work_queueinitializerinitargsexecutor	work_itemr   r   r   _workerE   s@   

r8   c                   @   s   e Zd ZdZdS )BrokenThreadPoolzR
    Raised when a worker thread in a ThreadPoolExecutor failed initializing.
    N)r#   r$   r%   __doc__r   r   r   r   r9   p   s    r9   c                   @   sd   e Zd Ze jZ		dddZdd Ze	j
jje_dd	 Zd
d ZdddddZe	j
jje_dS )ThreadPoolExecutorN r   c                 C   s   |du rt dt pdd }|dkrtd|dur#t|s#td|| _t | _	t
d| _t | _d| _d| _t
 | _|pGd	|   | _|| _|| _dS )
a  Initializes a new ThreadPoolExecutor instance.

        Args:
            max_workers: The maximum number of threads that can be used to
                execute the given calls.
            thread_name_prefix: An optional name prefix to give our threads.
            initializer: A callable used to initialize worker threads.
            initargs: A tuple of arguments to pass to the initializer.
        N          r   z"max_workers must be greater than 0zinitializer must be a callableFzThreadPoolExecutor-%d)minos	cpu_count
ValueErrorcallable	TypeError_max_workersqueueSimpleQueue_work_queue	threading	Semaphorer0   set_threads_brokenr   Lock_shutdown_lock_counter_thread_name_prefix_initializer	_initargs)r   max_workersthread_name_prefixr4   r5   r   r   r   r   {   s$   



zThreadPoolExecutor.__init__c             	   O   s   | j N t; | jrt| j| jrtdtrtdt }t||||}| j	
| |   |W  d    W  d    S 1 sDw   Y  W d    d S 1 sTw   Y  d S )Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown)rP   r   rN   r9   r   RuntimeErrorr   Futurer   rI   r	   _adjust_thread_count)r   r   r   r   fwr   r   r   submit   s   
RzThreadPoolExecutor.submitc                 C   s   | j jddr	d S | jfdd}t| j}|| jk rGd| jp| |f }tj|t	t
| || j| j| jfd}|  | j| | jt|< d S d S )Nr   )timeoutc                 S   s   | d  d S r   )r	   )_r   r   r   r   
weakref_cb   s   z;ThreadPoolExecutor._adjust_thread_count.<locals>.weakref_cbz%s_%d)nametargetr   )r0   acquirerI   lenrM   rF   rR   rJ   Threadr8   weakrefrefrS   rT   startaddr   )r   r_   num_threadsthread_namer   r   r   r   rY      s&   



z'ThreadPoolExecutor._adjust_thread_countc              	   C   st   | j - d| _	 z| j }W n
 tjy   Y nw |d ur'|jt| j qW d    d S 1 s3w   Y  d S )NzBA thread initializer failed, the thread pool is not usable anymore)	rP   rN   rI   
get_nowaitrG   Emptyr   r   r9   )r   r7   r   r   r   r.      s   "z&ThreadPoolExecutor._initializer_failedTF)cancel_futuresc             	   C   s   | j 0 d| _|r&	 z| j }W n
 tjy   Y nw |d ur%|j  q
| jd  W d    n1 s6w   Y  |rG| j	D ]}|
  q@d S d S r   )rP   r   rI   rk   rG   rl   r   cancelr	   rM   r
   )r   waitrm   r7   r   r   r   r   shutdown   s&   



zThreadPoolExecutor.shutdown)Nr<   Nr   )T)r#   r$   r%   	itertoolscount__next__rQ   r   r\   r   Executorr:   rY   r.   rp   r   r   r   r   r;   v   s    

&r;   )r:   
__author__concurrent.futuresr   rq   rG   rJ   r'   re   rA   WeakKeyDictionaryr   r   rO   r   r   _register_atexithasattrr   rb   _at_fork_reinitr1   objectr   r8   BrokenExecutorr9   rt   r;   r   r   r   r   <module>   s.   

+