
    .Pho*                         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 dgZ ej                    Zd adadad Z	 	 	 	 	 	 	 	 	 	 ddZ G d de          ZdS )    N   )ProcessPoolExecutorEXTRA_QUEUED_CALLS)	cpu_count)get_contextget_reusable_executorc                  j    t           5  t          } t          dz  a| cddd           S # 1 swxY w Y   dS )zEnsure that each successive executor instance has a unique, monotonic id.

    The purpose of this monotonic id is to help debug and test automated
    instance creation.
    r   N)_executor_lock_next_executor_id)executor_ids    g/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/joblib/externals/loky/reusable_executor.py_get_next_executor_idr      s     
  'Q                 s   (,,
   Fauto c
                 T    t                               | |||||||||	
  
        \  }
}|
S )a  Return the current ReusableExectutor instance.

    Start a new instance if it has not been started already or if the previous
    instance was left in a broken state.

    If the previous instance does not have the requested number of workers, the
    executor is dynamically resized to adjust the number of workers prior to
    returning.

    Reusing a singleton instance spares the overhead of starting new worker
    processes and importing common python packages each time.

    ``max_workers`` controls the maximum number of tasks that can be running in
    parallel in worker processes. By default this is set to the number of
    CPUs on the host.

    Setting ``timeout`` (in seconds) makes idle workers automatically shutdown
    so as to release system resources. New workers are respawn upon submission
    of new tasks so that ``max_workers`` are available to accept the newly
    submitted tasks. Setting ``timeout`` to around 100 times the time required
    to spawn new processes and import packages in them (on the order of 100ms)
    ensures that the overhead of spawning workers is negligible.

    Setting ``kill_workers=True`` makes it possible to forcibly interrupt
    previously spawned jobs to get a new instance of the reusable executor
    with new constructor argument values.

    The ``job_reducers`` and ``result_reducers`` are used to customize the
    pickling of tasks and results send to the executor.

    When provided, the ``initializer`` is run first in newly spawned
    processes with argument ``initargs``.

    The environment variable in the child process are a copy of the values in
    the main process. One can provide a dict ``{ENV: VAL}`` where ``ENV`` and
    ``VAL`` are string literals to overwrite the environment variable ``ENV``
    in the child processes to value ``VAL``. The environment variables are set
    in the children before any module is loaded. This only works with the
    ``loky`` context.
    )
max_workerscontexttimeoutkill_workersreusejob_reducersresult_reducersinitializerinitargsenv)_ReusablePoolExecutorr   )r   r   r   r   r   r   r   r   r   r   	_executor_s               r   r   r   %   sI    h )>>!!' ?  LIq     c                   ~     e Zd Z	 	 	 	 	 	 	 	 	 d fd	Ze	 	 	 	 	 	 	 	 	 	 dd            Z fd	Zd
 Zd Z fdZ	 xZ
S )r   Nr   r   c           
      v    t                                          |||||||	|
           || _        || _        d S )N)r   r   r   r   r   r   r   r   )super__init__r   _submit_resize_lock)selfsubmit_resize_lockr   r   r   r   r   r   r   r   r   	__class__s              r   r$   z_ReusablePoolExecutor.__init__i   sV     	#%+# 	 		
 		
 		
 '#5   r    r   Fr   c           
      &   t           5  t          }||du r
||j        }n(t                      }n|dk    rt	          d| d          t          |t                    rt          |          }|'|                                dk    rt	          d          t          ||||||	|
          }|Jd}t          j                            d	| d           t                      }|a | t           f||d
|xa}n|dk    r|t          k    }|j        j        s|j        j        s|r|j        |k     r|j        j        rd}n|j        j        rd}n|j        |k     rd}nd}t          j                            d| d| d           |                    d|           d xax}a | j        dd|i|cd d d            S t          j                            d|j         d           d}|                    |           d d d            n# 1 swxY w Y   ||fS )NTr   z(max_workers must be greater than 0, got .forkz4Cannot use reusable executor with the 'fork' context)r   r   r   r   r   r   r   Fz#Create a executor with max_workers=)r   r   r   brokenshutdownzqueue size is too smallzarguments have changedz)Creating a new executor with max_workers=z, as the previous instance cannot be reused (z).)waitr   r   z+Reusing existing executor with max_workers=r   )r
   r   _max_workersr   
ValueError
isinstancestrr   get_start_methoddictmputildebugr   _executor_kwargs_flagsr,   r-   
queue_sizer   _resize)clsr   r   r   r   r   r   r   r   r   r   executorkwargs	is_reusedr   reasons                   r   r   z+_ReusablePoolExecutor.get_reusable_executor   ss     O	2 O	2 H"D==X%9"*"7KK"+++KK!! M{MMM   '3'' /%g.."w'?'?'A'AV'K'K J   ) /'!  F !	H+HHH   455#) '*s"( + +( ( 	( ( 	HH F??"&66EO*!2/!2 !!2  *[88- 	:!)!1 :!+!,{:: ";!9GMM.&. .#). . .  
 %%4l%KKK>BBIB+;434  $/39 MO	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2T GMM@'/'<@ @ @   !%I$$[111_O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2 O	2b ""s   F$H9?HHHc                     | j         5   t                      j        |g|R i |cd d d            S # 1 swxY w Y   d S N)r%   r#   submit)r&   fnargsr>   r(   s       r   rC   z_ReusablePoolExecutor.submit   s    % 	7 	7!577>"6t666v66	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   377c                    | j         5  |t          d          || j        k    r	 d d d            d S | j        || _        	 d d d            d S |                                  | j        5  t          | j                                                  }t          d |D                       }|| _        t          ||          D ]}| j                            d            	 d d d            n# 1 swxY w Y   t          | j                  |k    rD| j        j        s8t!          j        d           t          | j                  |k    r| j        j        8|                                  t          | j                                                  }t'          d |D                       s-t!          j        d           t'          d |D                       -d d d            d S # 1 swxY w Y   d S )Nz&Trying to resize with max_workers=Nonec              3   >   K   | ]}|                                 V  d S rB   is_alive.0ps     r   	<genexpr>z0_ReusablePoolExecutor._resize.<locals>.<genexpr>   s*      'H'H

'H'H'H'H'H'Hr    MbP?c              3   >   K   | ]}|                                 V  d S rB   rH   rJ   s     r   rM   z0_ReusablePoolExecutor._resize.<locals>.<genexpr>  s*      ::1!**,,::::::r    )r%   r0   r/   _executor_manager_thread_wait_job_completion_processes_management_locklist
_processesvaluessumrange_call_queueputlenr9   r,   timesleep_adjust_process_countall)r&   r   	processesnb_children_aliver   s        r   r;   z_ReusablePoolExecutor._resize   s   %  	!  	!" !IJJJ 111	 	!  	!  	!  	!  	!  	!  	!  	! ,4 %0! 	!  	!  	!  	!  	!  	!  	!  	! %%'''
 0 / / !7!7!9!9::	$''H'Hi'H'H'H$H$H!$/!{,=>> / /A$((..../	/ / / / / / / / / / / / / / / DO$${224;;M2
4    DO$${224;;M2 &&(((T_335566I::	::::: !
4    ::	::::: !? 	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!  	!sB   GGG*A4C+G+C/	/G2C/	3CGG#&G#c                     | j         rBt          j        dt                     t          j                            d| j         d           | j         rt          j	        d           | j         dS dS )z8Wait for the cache to be empty before resizing the pool.z\Trying to resize an executor with running jobs: waiting for jobs completion before resizing.z	Executor z, waiting for jobs completion before resizingrN   N)
_pending_work_itemswarningswarnUserWarningr5   r6   r7   r   r[   r\   )r&   s    r   rQ   z*_ReusablePoolExecutor._wait_job_completion  s     # 		M?  
 GMM"D, " " "  
 & 	Jt & 	 	 	 	 	r    c                     t          t                      | j                  }d|z  t          z   | _        t                                          ||| j                   d S )N   )r:   )maxr   r/   r   r:   r#   _setup_queues)r&   r   r   min_queue_sizer(   s       r   ri   z#_ReusablePoolExecutor._setup_queues  sa     Y[[$*;<<n,/AA/do 	 	
 	
 	
 	
 	
r    )	NNNr   NNNr   N
NNr   Fr   NNNr   N)__name__
__module____qualname__r$   classmethodr   rC   r;   rQ   ri   __classcell__)r(   s   @r   r   r   h   s         6 6 6 6 6 64  ^# ^# ^# [^#@7 7 7 7 7!! !! !!F  "

 

 

 

 

 

 

 

 

r    r   rk   )r[   rc   	threadingmultiprocessingr5   process_executorr   r   backend.contextr   backendr   __all__RLockr
   r   r   r8   r   r   r   r   r    r   <module>rx      s             E E E E E E E E & & & & & &            "
# !"" 	 
 
 
 
@ @ @ @F~
 ~
 ~
 ~
 ~
/ ~
 ~
 ~
 ~
 ~
r    