
    g                         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	 Z eed
      xr  eej(                  ej*                         Zd Z G d de      Zd Zd ZddZy)    N)load)processutil)set_spawning_popen)Popen   )	reductionspawnr   c                     | |k(  xs? t         j                  j                  |       t         j                  j                  |      k(  S N)ospathnormcase)p1p2s     c/var/www/openai/venv/lib/python3.12/site-packages/joblib/externals/loky/backend/popen_loky_win32.py_path_eqr      s4    8Crww''+rww/?/?/CCC    _base_executablec                  <    | D ]  }t        j                  |        y r   )_winapiCloseHandle)handleshandles     r   _close_handlesr      s    F# r   c                       e Zd ZdZdZd Zy)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.
    lokyc                 n   t        j                  |j                  t        |dd            }t	        j
                  d d      \  }}t        j                  |d      }t        t        j                         |      }t        j                         }i t        j                  |j                  }t        rBt        |t         j"                        r(t         j$                  x|d<   }t         j"                  |d<   dj'                  d |D              }t)        |d      5 }		 t	        j*                  ||d d d	d|d d 	      \  }
}}}t	        j,                  |       || _        d | _        |
| _        t7        |
      | _        t;        j<                  | t>        | j8                  t7        |      f      | _         tC        |        	 tE        jF                  ||	       tE        jF                  ||	       tC        d        	 d d d        y # t.        $ r t	        j,                  |        w xY w# tC        d        w xY w# 1 sw Y   y xY w)
Ninit_main_moduleTr   )
parent_pidpipe_handle__PYVENV_LAUNCHER__ c              3   (   K   | ]
  }d | d   yw)"N ).0xs     r   	<genexpr>z!Popen.__init__.<locals>.<genexpr>Q   s     -A1#Qxs   wbF)$r
   get_preparation_data_namegetattrr   
CreatePipemsvcrtopen_osfhandleget_command_liner   getpidget_executableenvironenvWINENVr   sys
executabler   joinopenCreateProcessr   BaseExceptionpid
returncode_handleintsentinelr   Finalizer   	finalizerr   r	   dump)selfprocess_obj	prep_datarhandlewhandlewfdcmd
python_exe	child_envto_childhphtr=   _s                 r   __init__zPopen.__init__7   s   ..w{4FM
	 #--dA6##GQ/"))+7K))+
 6rzz5[__5	 hz3>>:"%"6"66CFZ/2~~I+,hh---#t_!(!6!6
"BQ ##B' DH"DODLGDM!]]nt}}c'l&CDN
 t$)y(3{H5"4(E _ ! ##G,& #4(E _s=   H+7G8A%H+7,H#H+8 HH+H((H++H4N)__name__
__module____qualname____doc__methodrR   r&   r   r   r   r   (   s    
 F>)r   c                     t        t        dd      rt        j                  d| gS d|  d| d}t        j                         }t        j                         g|d|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-c)r-   r7   r8   r   _args_from_interpreter_flagsr
   r3   )r!   r    kwdsprogoptss        r   r1   r1   x   s    sHe$ 8+FF  +}M*QH 	 002  "

 
 	

 %
 	
r   c                 2    t        |       dk\  r	| d   dk(  ryy)z4Return whether commandline indicates we are forking.   r   rZ   TF)len)argvs    r   
is_forkingrd      s    
4yA~$q'%==r   c                    t        t        j                        sJ d       |7t        j                  t        j
                  t        j                  z  d|      }nd}t        j                  | |      }t        j                  |t        j                        }|}t        j                  |dd      5 }dt        j                         _        	 t#        |      }t%        j&                  ||       t#        |      }t        j                         `	 ddd       j)                  |      }	t        j*                  |	       y# t        j                         `w xY w# 1 sw Y   IxY w)z.Run code specified by data received over pipe.zNot forkingNF)source_processrbT)closefd)rd   r7   rc   r   OpenProcessSYNCHRONIZEPROCESS_DUP_HANDLEr	   	duplicater/   r0   r   O_RDONLYfdopenr   current_process_inheritingr   r
   prepare
_bootstrapexit)
r!   r    rf   
new_handlefdparent_sentinelfrom_parentpreparation_datarE   exitcodes
             r   mainrz      s   chh.. ,,'"<"<<eZ
 $$NJ 
		z2;;	7B$O	2tT	*k04!-	6#K0MM*O<$D'')5 
+ /HHHX '')5 
+	*s$   1E,D<8E<EEEr   )r   r7   r/   r   pickler   multiprocessingr   r   multiprocessing.contextr   !multiprocessing.popen_spawn_win32r   _Popen r	   r
   __all__r   hasattrr8   r   r6   r   r1   rd   rz   r&   r   r   <module>r      s    	 
    ) 6 =  )D 
(	) 
(NNC((3 /
$M)F M)`
&r   