a
    Ÿ˜ëjÍ  ã                   @   sÂ   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mZ d dl	m
Z
 d dlmZ ddlmZmZ dgZd	d
„ ZeedƒoŠeejejƒ Zdd„ ZG dd„ deƒZdd„ Zdd„ Zddd„ZdS )é    N)Úload)ÚprocessÚutil)Úset_spawning_popen)ÚPopené   )Ú	reductionÚspawnr   c                 C   s    | |kpt j | ¡t j |¡kS ©N)ÚosÚpathÚnormcase)Úp1Úp2© r   úm/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/joblib/externals/loky/backend/popen_loky_win32.pyÚ_path_eq   s    r   Ú_base_executablec                  G   s   | D ]}t  |¡ qd S r
   )Ú_winapiÚCloseHandle)ZhandlesÚhandler   r   r   Ú_close_handles   s    r   c                   @   s   e Zd ZdZdZdd„ ZdS )r   a¹  
    Start a subprocess to run the code of a process object.

    We differ from cpython implementation with the way we handle environment
    variables, in order to be able to modify then in the child processes before
    importing any library, in order to control the number of threads in C-level
    threadpools.

    We also use the loky preparation data, in particular to handle main_module
    inits and the loky resource tracker.
    Zlokyc                 C   s„  t  |jt|ddƒ¡}t d d¡\}}t |d¡}tt	 
¡ |d�}t  ¡ }i t	j¥|j¥}tr‚t|tjƒr‚tj |d< }tj|d< d dd„ |D ƒ¡}t|d	ƒ�Î}	z0t ||d d d
d|d d ¡	\}
}}}t |¡ W n tyð   t |¡ ‚ Y n0 || _d | _|
| _t|
ƒ| _t | t| jt|ƒf¡| _ t!| ƒ z$t" #||	¡ t" #||	¡ W t!d ƒ n
t!d ƒ 0 W d   ƒ n1 �sv0    Y  d S )NZinit_main_moduleTr   )Ú
parent_pidÚpipe_handleÚ__PYVENV_LAUNCHER__ú c                 s   s   | ]}d |› d �V  qdS )ú"Nr   )Ú.0Úxr   r   r   Ú	<genexpr>Q   ó    z!Popen.__init__.<locals>.<genexpr>ÚwbF)$r	   Zget_preparation_dataÚ_nameÚgetattrr   Z
CreatePipeÚmsvcrtÚopen_osfhandleÚget_command_liner   ÚgetpidÚget_executableÚenvironÚenvÚWINENVr   ÚsysÚ
executabler   ÚjoinÚopenZCreateProcessr   ÚBaseExceptionÚpidÚ
returncodeÚ_handleÚintÚsentinelr   ZFinalizer   Ú	finalizerr   r   Údump)ÚselfZprocess_objZ	prep_dataZrhandleZwhandleZwfdÚcmdZ
python_exeZ	child_envZto_childÚhpZhtr1   Ú_r   r   r   Ú__init__7   sR    ÿ

÷

ÿzPopen.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmethodr<   r   r   r   r   r   (   s   c                 K   sP   t tddƒrtjd| gS d| › d|› d�}t ¡ }t ¡ g|¢d‘|‘d‘S dS )	zAReturns prefix of command line used for spawning a child process.ÚfrozenFú--multiprocessing-forkzRfrom joblib.externals.loky.backend.popen_loky_win32 import main; main(pipe_handle=z, parent_pid=ú)z-cN)r#   r,   r-   r   Ú_args_from_interpreter_flagsr	   r(   )r   r   ÚkwdsÚprogÚoptsr   r   r   r&   x   s&    ÿÿÿÿþýüûr&   c                 C   s$   t | ƒdkr| d dkrdS dS dS )z4Return whether commandline indicates we are forking.é   r   rC   TFN)Úlen)Úargvr   r   r   Ú
is_forking‹   s    rL   c           
   	   C   sÜ   t tjƒsJ dƒ‚|dur2t tjtjB d|¡}nd}tj| |d�}t	 
|tj¡}|}tj|ddd��N}dt ¡ _z(t|ƒ}t ||¡ t|ƒ}W t ¡ `n
t ¡ `0 W d  ƒ n1 sº0    Y  | |¡}	t |	¡ dS )z.Run code specified by data received over pipe.zNot forkingNF)Úsource_processÚrbT)Úclosefd)rL   r,   rK   r   ZOpenProcessZSYNCHRONIZEZPROCESS_DUP_HANDLEr   Z	duplicater$   r%   r   ÚO_RDONLYÚfdopenr   Úcurrent_processZ_inheritingr   r	   ÚprepareÚ
_bootstrapÚexit)
r   r   rM   Z
new_handleÚfdZparent_sentinelZfrom_parentZpreparation_datar8   Úexitcoder   r   r   Úmain“   s(    ÿÿ

2
rX   )N)r   r,   r$   r   Úpickler   Úmultiprocessingr   r   Zmultiprocessing.contextr   Z!multiprocessing.popen_spawn_win32r   Z_PopenÚ r   r	   Ú__all__r   Úhasattrr-   r   r+   r   r&   rL   rX   r   r   r   r   Ú<module>   s$   ÿP