a
    !f8                     @   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mZ d dlm	Z	 ddl
mZ ejdkrzd dlZd dlZd dlmZ g dZeed	ZejejfZeje jd
Ze jdkreed< dZG dd dZe ZejZejZej Z ej!Z!ej"Z"dddZ#dd Z$dS )    N)
sem_unlink)util   )spawnwin32)	duplicate)ensure_runningregister
unregisterpthread_sigmask)folderfileposixZsemlockFc                   @   sL   e Z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 )ResourceTrackerc                 C   s   t  | _d | _d | _d S N)	threadingLock_lock_fd_pidself r   o/var/www/html/python-backend/venv/lib/python3.9/site-packages/joblib/externals/loky/backend/resource_tracker.py__init__N   s    
zResourceTracker.__init__c                 C   s   |    | jS r   )r   r   r   r   r   r   getfdS   s    zResourceTracker.getfdc           	      C   s$  | j  | jdur~|  r,W d   dS t| j tjdkrhzt| jd W n tyf   Y n0 d| _d| _t	
d g }z|tj  W n ty   Y n0 t \}}tjdkrtt|dd}t| |}dtj d	| d
t d}zz|| t }|gt d|}td|  z8trLttj t! t"|||}W trttj#t! ntrttj#t! 0 W n  t$y   t|  Y n0 || _|| _W tjdkrt%&| n
t| n$tjdkrt%&| n
t| 0 W d   n1 s0    Y  dS )zMake sure that resource tracker process is running.

        This can be run from any process.  Usually a child process will use
        the resource created by its parent.Nr   r   z^resource_tracker: process died unexpectedly, relaunching.  Some folders/sempahores might leak.r   T)inheritablezfrom z import main; main(z, )z-czlaunching resource tracker: )'r   r   _check_aliveosclosenamewaitpidr   OSErrorwarningswarnappendsysstderrfileno	Exceptionpipeplatformr   msvcrtZget_osfhandlemain
__module__VERBOSEr   get_executabler   _args_from_interpreter_flagsdebug_HAVE_SIGMASKsignalr   	SIG_BLOCK_IGNORED_SIGNALSspawnv_passfdsSIG_UNBLOCKBaseException_winapiCloseHandle)	r   Zfds_to_passrw_rcmdexeargspidr   r   r   r   W   sp    







zResourceTracker.ensure_runningc                 C   s0   z|  ddd W n ty&   Y dS 0 dS dS )z8Check for the existence of the resource tracker process.PROBE FTN)_sendBrokenPipeErrorr   r   r   r   r      s
    zResourceTracker._check_alivec                 C   s   |    | d|| dS )z6Register a named resource, and increment its refcount.REGISTERNr   rF   r   r!   rtyper   r   r   r	      s    zResourceTracker.registerc                 C   s   |    | d|| dS )z2Unregister a named resource with resource tracker.
UNREGISTERNrI   rJ   r   r   r   r
      s    zResourceTracker.unregisterc                 C   s   |    | d|| dS )z@Decrement the refcount of a resource, and delete it if it hits 0MAYBE_UNLINKNrI   rJ   r   r   r   maybe_unlink   s    zResourceTracker.maybe_unlinkc                 C   sR   t |dkrtd| d| d| dd}t| j|}|t |ksNJ d S )Ni   zname too long:
ascii)len
ValueErrorencoder   writer   )r   r@   r!   rK   msgnbytesr   r   r   rF      s
    zResourceTracker._sendN)__name__r/   __qualname__r   r   r   r   r	   r
   rN   rF   r   r   r   r   r   M   s   P	r   c                    s   rt jt jd ttjtj ttjtj trDttj	t
 tjtjfD ]$}z|  W qP tyr   Y qP0 qP rt d dd t D }ztjdkrt| tj} t| d`}| }|dkrԐqz| dd	}|d
 d	|dd |d   }}}|dkr W q|tvrLtd| d| dtt  |dkr||| vrrd|| |< n|| |  d7  <  rt d| d| d|| |  d n|dkr|| |=  rt d| d| dt | d n|dkr|| |  d8  <  r>t d| d| d|| |  d || | d
kr|| |= z& rrt d|  t| | W n< ty }	 z"t!"d| d|	 W Y d}	~	n
d}	~	0 0 nt#d|W q t$y   ztj%t&   W n t$y   Y n0 Y q0 qW d   n1 s&0    Y  W  fd d!}
|' D ]$\}}|d"kr^qFn
|
|| qFd"|v r|
|d" d" nT fd d!}
|' D ]$\}}|d"krqn
|
|| qd"|v r|
|d" d" 0  rt d# dS )$zRun resource tracker.)levelz Main resource tracker is runningc                 S   s   i | ]
}|i qS r   r   ).0rK   r   r   r   
<dictcomp>       zmain.<locals>.<dictcomp>r   rbr]   rQ   rO   r   r   rD   zCannot register z/ for automatic cleanup: unknown resource type (z1). Resource type should be one of the following: rH   z*[ResourceTracker] incremented refcount of  z
 (current r   rL   z[ResourceTracker] unregister z: registry(rM   z*[ResourceTracker] decremented refcount of [ResourceTracker] unlink resource_tracker: : Nzunrecognized command c                    s   | r8z t dt|  d| d W n ty6   Y n0 | D ]d}z$t| |  rbtd|  W q< ty } z"t d| d| W Y d }~q<d }~0 0 q<d S )Nz%resource_tracker: There appear to be z leaked z  objects to clean up at shutdownra   rb   rc   )r$   r%   rR   r*   _CLEANUP_FUNCSr   r3   )rtype_registryrK   r!   everboser   r   _unlink_resources7  s$    zmain.<locals>._unlink_resourcesr   zresource tracker shut down)(r   Zlog_to_stderrDEBUGr5   SIGINTSIG_IGNSIGTERMr4   r   r9   r7   r'   stdinstdoutr    r*   r3   rd   keysr,   r-   open_osfhandler   O_RDONLYopenreadlinestripdecodesplitjoinrS   listrR   r$   r%   RuntimeErrorr:   
excepthookexc_infoitems)fdrh   fregistrylineZsplittedr@   r!   rK   rf   ri   re   r   rg   r   r.      s    












0



r.   c                    s   t |}tjdkrt \}}zJddlm  ddlm}  fdd|D }|||W t| t| S t| t| 0 n\d	dd	 |D }z0t
| |d d d
dd d d 	\}}	}
}t
|	 W n ty   Y n0 |
S d S )Nr   r   _mk_inheritable)	fork_execc                    s   g | ]} |qS r   r   )r[   r~   r   r   r   
<listcomp>l  r]   z"spawnv_passfds.<locals>.<listcomp>r`   c                 s   s   | ]}d | d V  qdS )"Nr   )r[   xr   r   r   	<genexpr>r  r]   z!spawnv_passfds.<locals>.<genexpr>Tr   )sortedr'   r,   r   r+   Z	reductionr   r   r    rx   r;   ZCreateProcessr<   r:   )pathrB   Zpassfdserrpipe_readerrpipe_writer   Z_passr@   _ZhtrC   r   r   r   r8   d  s0    



  
r8   )r   )%r   shutilr'   r5   r$   r   _multiprocessingr   multiprocessingr   rE   r   r,   r;   r-   Zmultiprocessing.reductionr   __all__hasattrr4   rk   rm   r7   rmtreeunlinkrd   r!   r0   r   Z_resource_trackerr   r	   rN   r
   r   r.   r8   r   r   r   r   <module>.   s:   


|
 