o
    l¨Êh&  ã                   @   s|  d dl mZ 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gZ
daejdd… dkrYd d	lmZ d d
lmZmZ d dlmZmZ e
dg7 Z
ejdkrSe
ddg7 Z
d'dd„Zn.ejdkrid dlZe ¡ ZdZnd dlmZ ejZdZeZdd„ Zdd„ Zdd„ Zd'dd„Zd(dd„Zdd „ Zd!d"„ ZG d#d$„ d$eƒZG d%d&„ d&eƒZ ejdkr¼	 eƒ Z!e!ej"j#d< e ƒ ej"j#d< dS dS ))é    )ÚdivisionNé   )ÚLokyProcessÚLokyInitMainProcessÚlokyÚloky_init_mainé   ©é   é   )Úget_context)Úassert_spawningÚset_spawning_popen)Úget_spawning_popenÚBaseContextÚspawnÚwin32ÚforkÚ
forkserverc                 C   sN   | pt pd} | dkrt dt¡ zt| ƒ}W |S  ty&   td | t¡ƒ‚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.ú,Unknown context '{}'. Value should be in {}.)Ú_DEFAULT_START_METHODÚwarningsÚwarnÚUserWarningÚmp_get_contextÚ
ValueErrorÚformatÚSTART_METHODS)ÚmethodÚcontext© r    úW/var/www/html/env/lib/python3.10/site-packages/joblib/externals/loky/backend/context.pyr   #   s   þ
üÿÿr   Úspawning_popen)ÚPopenÚprocess_handlec                   C   s   t ttd ƒS ©N)ÚgetattrÚ_tlsÚ
popen_attrr    r    r    r!   r   A   s   r   c                 C   s   t tt| ƒ d S r%   )Úsetattrr'   r(   )Úpopenr    r    r!   r   D   s   r   c                 C   s    t ƒ d u rtdt| ƒj ƒ‚d S )NzF%s objects should only be shared between processes through inheritance)r   ÚRuntimeErrorÚtypeÚ__name__)Úobjr    r    r!   r   G   s   
ÿÿÿr   c                 C   s8   | pt pd} | dkrtƒ S | dkrtƒ S td | t¡ƒ‚)Nr   r   r   )r   ÚLokyContextÚLokyInitMainContextr   r   r   )r   r    r    r!   r   N   s   ÿFc                 C   s<   t d ur
|s
tdƒ‚| d u s| tv sJ d | t¡ƒ‚| a d S )Nzcontext has already been setz4'{}' is not a valid start_method. It should be in {})r   r+   r   r   )r   Úforcer    r    r!   Úset_start_methodY   s   þr2   c                   C   s   t S r%   )r   r    r    r    r!   Úget_start_methodd   ó   r3   c                  C   s<  ddl } zt ¡ }W n ty   d}Y nw |}ttdƒr0z	tt d¡ƒ}W n	 ty/   Y nw |}d}d}tj 	|¡r‰tj 	|¡r‰t
|dƒ�}t| ¡ ƒ}W d  ƒ n1 sXw   Y  t
|dƒ�}t| ¡ ƒ}W d  ƒ n1 ssw   Y  |dkr‰|dkr‰t|  || ¡ƒ}ttj d|¡ƒ}	t||||	ƒ}
t|
dƒS )	au  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 with Python 3.4+ on some Unix systems);
     * CFS scheduler 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.
    It is also always larger or equal to 1.
    r   Nr   Úsched_getaffinityz#/sys/fs/cgroup/cpu/cpu.cfs_quota_usz$/sys/fs/cgroup/cpu/cpu.cfs_period_usÚrÚLOKY_MAX_CPU_COUNT)ÚmathÚmpÚ	cpu_countÚNotImplementedErrorÚhasattrÚosÚlenr5   ÚpathÚexistsÚopenÚintÚreadÚceilÚenvironÚgetÚminÚmax)r8   Úcpu_count_mpÚcpu_count_affinityÚcpu_count_cfsÚcfs_quota_fnameÚcfs_period_fnameÚfhÚcfs_quota_usÚcfs_period_usÚcpu_count_lokyÚaggregate_cpu_countr    r    r!   r:   h   s<   ÿ
ÿÿÿÿ
r:   c                   @   s  e Zd ZdZdZeZeeƒZd#dd„Z	d$dd„Z
ejdd	… d
k rd	 dd„ Zdd„ Zd%dd„Zejdkr:	 dd„ Zn*	 ddlmZ eejƒZeejƒZeejƒZeejƒZeejƒZeejƒZeejƒZejdkr†	 d&dd„Zdd„ Zdd„ Zdd„ Zd$dd „Zd!d"„ ZdS dS )'r/   z#Context relying on the LokyProcess.r   r   Nc                 C   s   ddl m} ||||  ¡ d�S )úReturns a queue objectr   )ÚQueue©ÚreducersÚctx)ÚqueuesrT   r   )ÚselfÚmaxsizerV   rT   r    r    r!   rT   ¢   s   ÿzLokyContext.Queuec                 C   s   ddl m} |||  ¡ d�S )rS   r   )ÚSimpleQueuerU   )rX   r[   r   )rY   rV   r[   r    r    r!   r[   ¨   s   zLokyContext.SimpleQueuer   r	   c                 C   s   | S r%   r    ©rY   r    r    r!   r   ¯   r4   zLokyContext.get_contextc                 C   s   | j S r%   )Ú_namer\   r    r    r!   r3   ²   s   zLokyContext.get_start_methodTc                 C   s
   t  |¡S )z1Returns two connection object connected by a pipe)r9   ÚPipe)rY   Úduplexr    r    r!   r^   µ   s   
zLokyContext.Piper   c                 C   s   ddl m} |ƒ }| ¡  |S )zReturns a manager objectr   )ÚLokyManager)Úmanagersr`   Ústart)rY   r`   Úmr    r    r!   ÚManager½   s   zLokyContext.Manager)Úsynchronizer   c                 C   s   ddl m} ||d�S )zReturns a semaphore objectr   )Ú	Semaphore)Úvalue)re   rf   )rY   rg   rf   r    r    r!   rf   Ô   s   
zLokyContext.Semaphorec                 C   ó   ddl m} ||ƒS )z"Returns a bounded semaphore objectr   )ÚBoundedSemaphore)re   ri   )rY   rg   ri   r    r    r!   ri   Ù   ó   zLokyContext.BoundedSemaphorec                 C   ó   ddl m} |ƒ S )zReturns a lock objectr   )ÚLock)re   rl   )rY   rl   r    r    r!   rl   Þ   ó   zLokyContext.Lockc                 C   rk   )zReturns a recurrent lock objectr   )ÚRLock)re   rn   )rY   rn   r    r    r!   rn   ã   rm   zLokyContext.RLockc                 C   rh   )zReturns a condition objectr   )Ú	Condition)re   ro   )rY   Úlockro   r    r    r!   ro   è   rj   zLokyContext.Conditionc                 C   rk   )zReturns an event objectr   )ÚEvent)re   rq   )rY   rq   r    r    r!   rq   í   rm   zLokyContext.Event)r   Nr%   )T)r   )r-   Ú
__module__Ú__qualname__Ú__doc__r]   r   ÚProcessÚstaticmethodr:   rT   r[   ÚsysÚversion_infor   r3   r^   Úplatformrd   Úmultiprocessingre   rf   ri   rl   rn   ro   rq   r9   r    r    r    r!   r/   œ   sB    














ãr/   c                   @   s   e Zd ZdZdZeZdS )r0   aû  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 python3.4+.
    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-   rr   rs   rt   r]   r   ru   r    r    r    r!   r0   ó   s    r0   r%   )F)$Ú
__future__r   r=   rw   r   rz   r9   Úprocessr   r   r   r   rx   r   r   Úmultiprocessing.contextr   r   r   r   ry   Ú	threadingÚlocalr'   r(   Úmultiprocessing.forkingr#   Úobjectr2   r3   r:   r/   r0   Úctx_lokyr   Ú_concrete_contextsr    r    r    r!   Ú<module>   sN   




4W
ü