o
    tBh;                     @   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	y1   dZY nw ej
dkZdd ZdddZdd	d
Zdd Zdd Zdd Zdd Zdd ZdS )    Nwin32c                  C   s   t  } d| _dS )z0Put a ``_clean_exit`` flag on the current threadTN)	threadingcurrent_thread_clean_exit)thread r   z/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/joblib/externals/loky/backend/utils.py_flag_current_thread_clean_exit   s   
r	   Tc                 C   s$   |rt d urt|  d S t|  d S N)psutil _recursive_terminate_with_psutil#_recursive_terminate_without_psutil)process
use_psutilr   r   r   recursive_terminate   s   r      c              	   C   sx   zt | jjdd}W n t jy   Y d S w |d d d D ]}z|  W q t jy1   Y qw |   |   d S )NT)	recursive)r   ProcesspidchildrenNoSuchProcesskill	terminatejoin)r   retriesr   childr   r   r   r      s   r   c              
   C   sR   zt | j W n ty" } ztd |   W Y d}~nd}~ww |   dS )z-Terminate a process and its descendants.
    zgFailed to kill subprocesses on this platform. Pleaseinstall psutil: https://github.com/giampaolo/psutilN)_recursive_terminater   OSErrorwarningswarnr   r   )r   er   r   r   r   1   s   
r   c                 C   s  t jdkrgztjddddt| gdd W dS  tjyf } zC|jdvr% |jd	krMz	t| t	j
 W n  tyL } z|jtjkrB W Y d}~nd}~ww W Y d}~dS W Y d}~dS W Y d}~dS d}~ww ztjd
dt| gdd}W n tjy } z|jd	krd}n W Y d}~nd}~ww | ddd }|D ]
}t|}t| qz
t| t	j
 W dS  ty } z|jtjkrÂ W Y d}~dS d}~ww )zERecursively kill the descendants of a process before killing it.
    r   taskkillz/Fz/Tz/PIDN)stderr)         r$   pgrepz-P    
r   )sysplatform
subprocesscheck_outputstrCalledProcessError
returncodeosr   signalSIGTERMr   errnoESRCHdecodesplitintr   )r   r!   children_pidscpidr   r   r   r   ?   s^   





r   c                 C   sr   d}dd t |  D }t|dkr5|dkr5|d8 }dd t |  D }td t|dkr5|dkst|S )zReturn a formated 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.
    r   c                 S      g | ]
}|j d ur|j qS r
   exitcode.0pr   r   r   
<listcomp>       
z3get_exitcodes_terminated_worker.<locals>.<listcomp>r   r$   c                 S   r;   r
   r<   r>   r   r   r   rA      rB   g?)listvalueslentimesleep_format_exitcodes)	processespatience	exitcodesr   r   r   get_exitcodes_terminated_workerw   s   
rL   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 urd t||qS )Nz{}({}))format_get_exitcode_name)r?   r!   r   r   r   rA      s    
z%_format_exitcodes.<locals>.<listcomp>{z, })r   )rK   str_exitcodesr   r   r   rH      s   rH   c                 C   s   t jdkrdS | dk rHz2dd l}t jdkr||  jW S |j D ]\}}|dr:|ds:||  kr:|  W S q"W dS  t	yG   Y dS w | dkrNdS dS )	Nr   UNKNOWNr   )   r   SIGSIG_r&   EXIT)
r*   r+   r2   version_infoSignalsname__dict__items
startswith
ValueError)r=   r2   vkr   r   r   rN      s(   



rN   )T)r   )r1   r*   rF   r4   r2   r   r   r,   r   ImportErrorr+   WIN32r	   r   r   r   r   rL   rH   rN   r   r   r   r   <module>   s,    


8