
    7Tho*                         S SK r S SKrS SKrS SKrSSKJrJr  SSKJ	r	  SSK
Jr  S/r\R                  " 5       rS qSqSqS r          SS jr " S	 S
\5      rg)    N   )ProcessPoolExecutorEXTRA_QUEUED_CALLS)	cpu_count)get_contextget_reusable_executorc                  f    [            [        n [        S-  qU sSSS5        $ ! , (       d  f       g= f)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    _/var/www/auris/envauris/lib/python3.13/site-packages/joblib/externals/loky/reusable_executor.py_get_next_executor_idr      s$     
'Q 
s   "
0c
                 B    [         R                  U UUUUUUUUU	S9
u  pU
$ )a4  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   %   sB    h )>>!!' ? LI     c                      ^  \ rS rSr         S	U 4S jjr\          S
S j5       rU 4S jrS rS r	U 4S jr
SrU =r$ )r   h   c                 F   > [         TU ]  UUUUUUU	U
S9  XPl        Xl        g )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"   _ReusablePoolExecutor.__init__i   s=     	#%+# 	 		
 '#5 r   c                    [            [        nUc   USL a  Ub  UR                  nO [        5       nOUS::  a  [	        SU S35      e[        U[        5      (       a  [        U5      nUb  UR                  5       S:X  a  [	        S5      e[        UUUUUU	U
S9nUcE  Sn[        R                  R                  S	U S35        [        5       nUqU " [         4UUS
.UD6=qnGOFUS:X  a	  U[        :H  nUR                  R                   (       d2  UR                  R"                  (       d  U(       a  UR$                  U:  a  UR                  R                   (       a  SnO3UR                  R"                  (       a  SnOUR$                  U:  a  SnOSn[        R                  R                  SU SU S35        UR#                  SUS9  S =q=nqU R&                  " SSU0UD6sS S S 5        $ [        R                  R                  SUR                   S35        SnUR)                  U5        S S S 5        X4$ ! , (       d  f       WW4$ = f)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   auto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   _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   +_ReusablePoolExecutor.get_reusable_executor   sn     H"D=X%9"*"7"7K"++K! >{m1M  '3''%g."w'?'?'AV'K J  ) /'!F !	9+aH 45#) '*"( + +( 	( 	H F?"&66EOO**// **[8--!)!11!+!,,{: ";!9GGMMC&- (##)(".
 %%4l%K>BBIB+;44 $/39M ^T GGMM''/'<'<&=Q@ !%I$$[1_ b ""c ^b ""s   GH0%A H00
Ic                 z   > U R                      [        TU ]  " U/UQ70 UD6sS S S 5        $ ! , (       d  f       g = fN)r#   r!   submit)r$   fnargsr?   r&   s       r   rE   _ReusablePoolExecutor.submit   s.    %%7>"6t6v6 &%%s   ,
:c                    U R                      Uc  [        S5      eXR                  :X  a
   S S S 5        g U R                  c  Xl         S S S 5        g U R	                  5         U R
                     [        U R                  R                  5       5      n[        S U 5       5      nXl        [        X5       H  nU R                  R                  S 5        M      S S S 5        [        U R                  5      U:  ag  U R                  R                  (       dL  [         R"                  " S5        [        U R                  5      U:  a  U R                  R                  (       d  ML  U R%                  5         [        U R                  R                  5       5      n['        S U 5       5      (       d/  [         R"                  " S5        ['        S U 5       5      (       d  M/  S S S 5        g ! , (       d  f       GN= f! , (       d  f       g = f)Nz&Trying to resize with max_workers=Nonec              3   @   #    U  H  oR                  5       v   M     g 7frD   is_alive.0ps     r   	<genexpr>0_ReusablePoolExecutor._resize.<locals>.<genexpr>   s     'Hi

i   MbP?c              3   @   #    U  H  oR                  5       v   M     g 7frD   rK   rM   s     r   rP   rQ     s     :	1**,,	rR   )r#   r1   r0   _executor_manager_thread_wait_job_completion_processes_management_locklist
_processesvaluessumrange_call_queueputlenr:   r,   timesleep_adjust_process_countall)r$   r   	processesnb_children_aliver   s        r   r<   _ReusablePoolExecutor._resize   s   %%" !IJJ 1 11	 &% ,,4 %0! &% %%'
 00 !7!7!9:	$''Hi'H$H!$/!{>A$$((. ?	 1 DOO${24;;;M;M

4  DOO${24;;;M;M;M &&(T__3356I:	:::

4  :	:::? &%$ 10% &%s7   G3G3G3.A)G!BG3A7G3!
G0	+G33
Hc                 ,   U R                   (       aH  [        R                  " S[        5        [        R
                  R                  SU R                   S35        U R                   (       a*  [        R                  " S5        U R                   (       a  M)  gg)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 resizingrS   N)
_pending_work_itemswarningswarnUserWarningr6   r7   r8   r   r`   ra   )r$   s    r   rV   *_ReusablePoolExecutor._wait_job_completion  sp     ##MM?
 GGMMD,,- ." "
 &&JJt &&&r   c                    > [        [        5       U R                  5      nSU-  [        -   U l        [
        TU ]  XU R                  S9  g )N   )r;   )maxr   r0   r   r;   r!   _setup_queues)r$   r   r   min_queue_sizer&   s       r   rp   #_ReusablePoolExecutor._setup_queues  sF     Y[$*;*;<n,/AAdoo 	 	
r   )r0   r#   r   r;   )	NNNr   NNNr/   N
NN
   Fr+   NNNr/   N)__name__
__module____qualname____firstlineno__r"   classmethodr   rE   r<   rV   rp   __static_attributes____classcell__)r&   s   @r   r   r   h   sv     64  ^# ^#@7!!F"

 

r   r   rs   )r`   ri   	threadingmultiprocessingr6   process_executorr   r   backend.contextr   backendr   __all__RLockr
   r   r   r9   r   r   r   r/   r   r   <module>r      s~        E &  "
# " 	 
 
@F~
/ ~
r   