
    .Ph7                        d dl 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d dlmZ	 d dl
mZ d dlmZ ddlmZmZ ej        dk     redz
  Zg dZej        d	k    red
dgz  ZdadaddZddZd ZddZd Zd Zd Zd Zd Zd Zd Z G d de          Z  G d de           Z! e             Z"e"ej#        j$        d<    e!            ej#        j$        d<   dS )     N)get_context)BaseContext)_MAX_WINDOWS_WORKERS   )LokyProcessLokyInitMainProcess)   
   )lokyloky_init_mainspawnwin32fork
forkserverc                     | pt           pd} | dk    rt          j        dt                     	 t	          |           S # t
          $ r t          d|  dt           d          w xY w)Nr   r   zu`fork` start method should not be used with `loky` as it does not respect POSIX. Try using `spawn` or `loky` instead.zUnknown context 'z'. Value should be in .)_DEFAULT_START_METHODwarningswarnUserWarningmp_get_context
ValueErrorSTART_METHODS)methods    e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/joblib/externals/loky/backend/context.pyr   r   *   s    6,6F) 		
 	
 	

f%%% 
 
 
          
 
 	

s	   < &A"Fc                 z    t           |st          d          | | t          v sJ d|  dt                       | a d S )Nzcontext has already been set'z/' is not a valid start_method. It should be in )r   RuntimeErrorr   )r   forces     r   set_start_methodr    >   se    ((9:::>V}444	F 	 		 	 544
 #    c                      t           S N)r    r!   r   get_start_methodr%   J   s      r!   c                    t          j                    pd}t          j        dk    rt	          |t
                    }t          |          }t          t	          ||          d          }| s|S ||k     rt          |d          S t                      \  }}|dk    r|S |1t          j
        d| d           t          j        |j                   |S )a  Return the number of CPUs the current process can use.

    The returned number of CPUs accounts for:
     * the number of CPUs in the system, as given by
       ``multiprocessing.cpu_count``;
     * the CPU affinity settings of the current process
       (available on some Unix systems);
     * Cgroup CPU bandwidth limit (available on Linux only, typically
       set by docker and similar container orchestration systems);
     * the value of the LOKY_MAX_CPU_COUNT environment variable if defined.
    and is given as the minimum of these constraints.

    If ``only_physical_cores`` is True, return the number of physical cores
    instead of the number of logical cores (hyperthreading / SMT). Note that
    this option is not enforced if the number of usable cores is controlled in
    any other way such as: process affinity, Cgroup restricted CPU bandwidth
    or the LOKY_MAX_CPU_COUNT environment variable. If the number of physical
    cores is not found, return the number of logical cores.

    Note that on Windows, the returned number of CPUs cannot exceed 61 (or 60 for
    Python < 3.10), see:
    https://bugs.python.org/issue26903.

    It is also always larger or equal to 1.
    r   r   	not foundNzFCould not find the number of physical cores for the following reason:
z
Returning the number of logical cores instead. You can silence this warning by setting LOKY_MAX_CPU_COUNT to the number of cores you want to use.)os	cpu_countsysplatformminr   _cpu_count_usermax_count_physical_coresr   r   	tracebackprint_tb__traceback__)only_physical_coresos_cpu_countcpu_count_useraggregate_cpu_countcpu_count_physical	exceptions         r   r)   r)   N   s    6 <>>&QL
|w <)=>>$\22Nc,??CC #""$$>1%%%$9$;$;!	[((!! 3"+3 3 3	
 	
 	
 	92333r!   c                 b   d}d}d}t           j                            |          rct          |          5 }|                                                                                                \  }}d d d            n# 1 swxY w Y   nt           j                            |          rt           j                            |          rt          |          5 }|                                                                }d d d            n# 1 swxY w Y   t          |          5 }|                                                                }d d d            n# 1 swxY w Y   nd}d}|dk    r| S t          |          }t          |          }|dk    r|dk    rt          j	        ||z            S | S )Nz/sys/fs/cgroup/cpu.maxz#/sys/fs/cgroup/cpu/cpu.cfs_quota_usz$/sys/fs/cgroup/cpu/cpu.cfs_period_usr.   i r   )
r(   pathexistsopenreadstripsplitintmathceil)r4   cpu_max_fnamecfs_quota_fnamecfs_period_fnamefhcpu_quota_uscpu_period_uss          r   _cpu_count_cgrouprI      sJ   ,M;O=	w~~m$$   -   	DB*,''))//*;*;*A*A*C*C'L-	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D		(	( 
 RW^^<L-M-M 
  /"" 	-b7799??,,L	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-"## 	.rGGIIOO--M	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. u<((M**! 1 19\M9:::  s5   <A==BB'D		DD#'EEEc                    t          t          d          r2	 t          t          j        d                    S # t          $ r Y nw xY w	 dd l}|                                }t          |d          r!t          |                                          S nS# t          $ rF t          j
        dk    r3t          j                            d          t          j        d           Y nw xY w| S )Nsched_getaffinityr   cpu_affinitylinuxLOKY_MAX_CPU_COUNTzuFailed to inspect CPU affinity constraints on this system. Please install psutil or explictly set LOKY_MAX_CPU_COUNT.)hasattrr(   lenrK   NotImplementedErrorpsutilProcessrL   ImportErrorr*   r+   environgetr   r   )r4   rR   ps      r   _cpu_count_affinityrX      s   r&'' 	r+A..///" 	 	 	D	
NN1n%% 	)q~~''(((	)    LG##
344< MM    s#    8 
AA	AB AC#"C#c                     t          |           }t          |           }t          t          j                            d|                     }t          |||          S )z%Number of user defined available CPUsrN   )rX   rI   r@   r(   rU   rV   r,   )r4   cpu_count_affinitycpu_count_cgroupcpu_count_lokys       r   r-   r-      sR    ,\::(66 (<lKKLLN!#3^DDDr!   c                     d} t           	t           | fS 	 t          j        dk    rt                      }nZt          j        dk    rt	                      }n;t          j        dk    rt                      }nt          dt          j                   |dk     rt          d| d          n# t          $ r}|} d	}Y d}~nd}~ww xY w|a || fS )
a  Return a tuple (number of physical cores, exception)

    If the number of physical cores is found, exception is set to None.
    If it has not been found, return ("not found", exception).

    The number of physical cores is cached to avoid repeating subprocess calls.
    NrM   r   darwinzunsupported platform: r   zfound z physical cores < 1r'   )	physical_cores_cacher*   r+   _count_physical_cores_linux_count_physical_cores_win32_count_physical_cores_darwinrQ   r   	Exception)r8   r7   es      r   r/   r/      s	    I '#Y..)<7""!<!>!>\W$$!<!>!>\X%%!=!?!?%&Ms|&M&MNNN !!M&8MMMNNN "  ) ) )	()
 .y((s   BB' '
B?1B::B?c                     	 t          j        d                                dd          } | j                                        } d | D             } t          |           S #  Y nxY wt          j        d                                dd          } | j                                        } d | D             } t          |           S )Nzlscpu --parse=coreTcapture_outputtextc                 <    h | ]}|                     d           |S )#
startswith.0lines     r   	<setcomp>z._count_physical_cores_linux.<locals>.<setcomp>  s)    JJJTT__S5I5IJDJJJr!   zcat /proc/cpuinfoc                 <    h | ]}|                     d           |S )zcore idrk   rm   s     r   rp   z._count_physical_cores_linux.<locals>.<setcomp>  s)    HHHT__Y-G-GHHHHr!   )
subprocessrunr?   stdout
splitlinesrP   cpu_infos    r   r`   r`     s    > &&((D
 
 
 ?--//JJXJJJ8}}~!!##Dt  H ))++HHHHHHHx==s   AA A#c                     	 d} t          j        d|                                  dd          }|j                                        }t          |d                   S #  Y nxY wt          j        d                                dd          }|j                                        }d |D             }t          t          t
          |                    S )NzC-Command (Get-CimInstance -ClassName Win32_Processor).NumberOfCoreszpowershell.exe Trf   r   z&wmic CPU Get NumberOfCores /Format:csvc                 T    g | ]%}|r!|d k    
|                     d          d         &S )zNode,NumberOfCores,r   )r?   )rn   ls     r   
<listcomp>z/_count_physical_cores_win32.<locals>.<listcomp>1  s@       a459M4M4MQ4M4M4Mr!   )rr   rs   r?   rt   ru   r@   summap)cmdrw   s     r   ra   ra     s    
S>#c##))++
 
 

 ?--//8A;~06688  H
 ))++H !)  H s3!!"""s   AA A"c                      t          j        d                                dd          } | j        } t	          |           S )Nzsysctl -n hw.physicalcpuTrf   )rr   rs   r?   rt   r@   rv   s    r   rb   rb   7  sB    ~"((**  H
 Hx==r!   c                       e Zd ZdZdZeZ ee          ZddZ	ddZ
ej        dk    r	 dd	Zd
 Zd Zd ZddZd ZdS dS )LokyContextz#Context relying on the LokyProcess.r   r   Nc                 N    ddl m}  ||||                                           S )Returns a queue objectr   )Queuereducersctx)queuesr   r   )selfmaxsizer   r   s       r   r   zLokyContext.QueueH  s7    !!!!!!uWxT5E5E5G5GHHHHr!   c                 L    ddl m}  |||                                           S )r   r   )SimpleQueuer   )r   r   r   )r   r   r   s      r   r   zLokyContext.SimpleQueueN  s5    ''''''{H$2B2B2D2DEEEEr!   r   r   c                 &    ddl m}  ||          S )zReturns a semaphore objectr   )	Semaphore)value)synchronizer   )r   r   r   s      r   r   zLokyContext.SemaphoreZ  s'    ......95))))r!   c                 $    ddl m}  ||          S )z"Returns a bounded semaphore objectr   )BoundedSemaphore)r   r   )r   r   r   s      r   r   zLokyContext.BoundedSemaphore`  s%    555555##E***r!   c                 "    ddl m}  |            S )zReturns a lock objectr   )Lock)r   r   )r   r   s     r   r   zLokyContext.Lockf  s    ))))))466Mr!   c                 "    ddl m}  |            S )zReturns a recurrent lock objectr   )RLock)r   r   )r   r   s     r   r   zLokyContext.RLockl      ******577Nr!   c                 $    ddl m}  ||          S )zReturns a condition objectr   )	Condition)r   r   )r   lockr   s      r   r   zLokyContext.Conditionr  s"    ......9T??"r!   c                 "    ddl m}  |            S )zReturns an event objectr   )Event)r   r   )r   r   s     r   r   zLokyContext.Eventx  r   r!   )r   Nr#   )r   )__name__
__module____qualname____doc___namer   rS   staticmethodr)   r   r   r*   r+   r   r   r   r   r   r   r$   r!   r   r   r   A  s        --EGY''II I I IF F F F |w	
	* 	* 	* 	*	+ 	+ 	+	 	 		 	 		# 	# 	# 	#	 	 	 	 	I r!   r   c                       e Zd ZdZdZeZdS )LokyInitMainContexta  Extra context with LokyProcess, which does load the main module

    This context is used for compatibility in the case ``cloudpickle`` is not
    present on the running system. This permits to load functions defined in
    the ``main`` module, using proper safeguards. The declaration of the
    ``executor`` should be protected by ``if __name__ == "__main__":`` and the
    functions and variable used from main should be out of this block.

    This mimics the default behavior of multiprocessing under Windows and the
    behavior of the ``spawn`` start method on a posix system.
    For more details, see the end of the following section of python doc
    https://docs.python.org/3/library/multiprocessing.html#multiprocessing-programming
    r   N)r   r   r   r   r   r   rS   r$   r!   r   r   r     s$          E!GGGr!   r   r   r   r#   )F)%r(   r*   rA   rr   r0   r   multiprocessingmpr   r   multiprocessing.contextr   concurrent.futures.processr   processr   r   version_infor   r+   r   r_   r    r%   r)   rI   rX   r-   r/   r`   ra   rb   r   r   ctx_lokycontext_concrete_contextsr$   r!   r   <module>r      s:   
			 



               9 9 9 9 9 9 / / / / / / ; ; ; ; ; ; 6 5 5 5 5 5 5 5 g/!3333<7fl++M   
 
 
 
(	# 	# 	# 	#! ! !> > > >B!  !  ! H! ! !H	E 	E 	E%) %) %)P  &# # #2  ; ; ; ; ;+ ; ; ;|" " " " "+ " " "( ;==(0
 f %2E2E2G2G
 . / / /r!   