U
    e}                     @   s   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Zzd dlZW n e	k
rd   dZY nX dddZ
dddZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    NTc                 C   s"   |rt dk	rt|  nt|  dS )z2Terminate process and its descendants with SIGKILLN)psutil_kill_process_tree_with_psutil!_kill_process_tree_without_psutilprocess
use_psutil r   d/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/joblib/externals/loky/backend/utils.pykill_process_tree   s    
r
   c                 C   s   t dt t| |d d S )NzKrecursive_terminate is deprecated in loky 3.2, use kill_process_treeinstead)r   )warningswarnDeprecationWarningr
   r   r   r   r	   recursive_terminate   s
    r   c              	   C   s   zt | jjdd}W n t jk
r0   Y d S X |d d d D ](}z|  W q@ t jk
rf   Y q@X q@zt | j  W n t jk
r   Y nX |   d S )NT)	recursive)r   ProcesspidchildrenZNoSuchProcesskilljoin)r   ZdescendantsZ
descendantr   r   r	   r   !   s    r   c                 C   sf   z$t jdkrt| j n
t| j W n4 tk
rX   t }t	d|  | 
  Y nX |   dS )z(Terminate a process and its descendants.win32zqFailed to kill subprocesses on this platform. Please installpsutil: https://github.com/giampaolo/psutil
Details:
N)sysplatform_windows_taskkill_process_treer   _posix_recursive_kill	Exception	traceback
format_excr   r   r   r   )r   detailsr   r   r	   r   6   s    

r   c              
   C   sV   z t jddddt| gd d W n0 t jk
rP } z|jdkr@ W 5 d }~X Y nX d S )NZtaskkillz/Fz/Tz/PID)stderr)      )
subprocesscheck_outputstrCalledProcessError
returncode)r   er   r   r	   r   M   s     

r   c              
   C   sT   t tdtj}zt| | W n0 tk
rN } z|jtjkr> W 5 d }~X Y nX d S )NSIGKILL)getattrsignalSIGTERMosr   OSErrorerrnoZESRCH)r   Zkill_signalr'   r   r   r	   _kill\   s    r/   c              
   C   s   zt jddt| gddd}W n6 t jk
rT } z|jdkrBd}n W 5 d}~X Y nX | D ]}t|}t| q^t|  dS )z@Recursively kill the descendants of a process before killing it.Zpgrepz-PNT)r   text    )	r"   r#   r$   r%   r&   
splitlinesintr   r/   )r   Zchildren_pidsr'   Zcpidr   r   r	   r   l   s      


r   c                 C   sX   d}dd t |  D }|sP|dkrP|d8 }dd t |  D }td qt|S )zReturn a formatted string with the exitcodes of terminated workers.

    If necessary, wait (up to .25s) for the system to correctly set the
    exitcode of one terminated worker.
       c                 S   s   g | ]}|j d k	r|j qS Nexitcode.0pr   r   r	   
<listcomp>   s    
 z3get_exitcodes_terminated_worker.<locals>.<listcomp>r   r1   c                 S   s   g | ]}|j d k	r|j qS r6   r7   r9   r   r   r	   r<      s   
g?)listvaluestimesleep_format_exitcodes)Z	processesZpatience	exitcodesr   r   r	   get_exitcodes_terminated_worker   s    

rC   c                 C   s    dd | D }dd | d S )z@Format a list of exit code with names of the signals if possiblec                 S   s(   g | ] }|d k	rt | d| dqS )N())_get_exitcode_name)r:   r'   r   r   r	   r<      s     z%_format_exitcodes.<locals>.<listcomp>{z, })r   )rB   Zstr_exitcodesr   r   r	   rA      s    rA   c                 C   sX   t jdkrdS | dk rHzdd l}||  jW S  tk
rD   Y dS X n| dkrTdS dS )Nr   UNKNOWNr   r!   ZEXIT)r   r   r*   Signalsname
ValueError)r8   r*   r   r   r	   rF      s    

rF   )T)T)r,   r   r?   r.   r*   r   r"   r   r   ImportErrorr
   r   r   r   r   r/   r   rC   rA   rF   r   r   r   r	   <module>   s*   


	