
    Ǆg.                     8   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d dlZd dl	Z	d dl
mZmZ d dlmZ ddlmZ dZ e j$                  e      Zg dZ G d d	e      Z G d
 de      Z G d de      Zd Z G d d      Z G d de      Z	 	 	 	 	 ddZddZy)    N)as_completedThreadPoolExecutor)Optional   )_prctl_pr_set_pdeathsigTORCH_MP_PARALLEL_START)ProcessContextProcessExceptionProcessExitedExceptionProcessRaisedExceptionspawnSpawnContextstart_processesc                   :     e Zd ZddgZdededef fdZd Z xZS )r
   error_index	error_pidmsgpidc                 N    t         |   |       || _        || _        || _        y N)super__init__r   r   r   )selfr   r   r   	__class__s       c/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/multiprocessing/spawn.pyr   zProcessException.__init__$   s&    &    c                 ^    t        |       | j                  | j                  | j                  ffS r   )typer   r   r   r   s    r   
__reduce__zProcessException.__reduce__*   s&    DzDHHd&6&6AAAr   )	__name__
__module____qualname__	__slots__strintr   r    __classcell__r   s   @r   r
   r
   !   s-    ,IC c  Br   r
   c                   0     e Zd ZdZdededef fdZ xZS )r   zNException raised when a process failed due to an exception raised by the code.r   r   r   c                 (    t         |   |||       y r   )r   r   )r   r   r   r   r   s       r   r   zProcessRaisedException.__init__1   s     	k95r   )r!   r"   r#   __doc__r%   r&   r   r'   r(   s   @r   r   r   .   s,    X66 6 	6 6r   r   c                   N     e Zd ZdZdgZ	 d	dededededee   f
 fdZd Z	 xZ
S )
r   zTException raised when a process failed due to signal or exited with a specific code.	exit_coder   r   r   signal_namec                 D    t         |   |||       || _        || _        y r   )r   r   r-   r.   )r   r   r   r   r-   r.   r   s         r   r   zProcessExitedException.__init__?   s%     	k95"&r   c                     t        |       | j                  | j                  | j                  | j                  | j
                  ffS r   )r   r   r   r   r-   r.   r   s    r   r    z!ProcessExitedException.__reduce__K   s9    JXXt''4>>4CSCST
 	
r   r   )r!   r"   r#   r+   r$   r%   r&   r   r   r    r'   r(   s   @r   r   r   :   sP    ^I &*
'
' 
' 	
'
 
' c]
'
r   r   c                 :   t        t        j                         	  | |g|  y # t        $ r Y y t        $ rb dd l}t        |d      5 }t        j                  |j                         |       d d d        n# 1 sw Y   nxY wt        j                  d       Y y w xY w)Nr   wbr   )r   signalSIGINTKeyboardInterrupt	Exception	tracebackopenpickledump
format_excsysexit)fniargs
error_filer7   fhs         r   _wraprC   R   s    
 FMM*

1t  *d# 	4rKK	,,.3	4 	4 	4s,   	% 	BB%A6-	B6A?	;BBc                        e Zd Zd Zd ZddZy)r	   c                     || _         || _        t        |      D ci c]  \  }}|j                  | c}}| _        y c c}}w r   )error_files	processes	enumeratesentinel	sentinels)r   rG   rF   indexprocesss        r   r   zProcessContext.__init__g   sA    &":CI:N
(6wGe#
 
s   >c                 f    | j                   D cg c]  }t        |j                         c}S c c}w r   )rG   r&   r   )r   rL   s     r   pidszProcessContext.pidsn   s#    04?WGKK ???s   .Nc                    t        | j                        dk(  ryt        j                  j	                  | j                  j                         |      }d}|D ]O  }| j                  j                  |      }| j                  |   }|j                          |j                  dk7  sM|} n |t        | j                        dk(  S d}| j                  D ]C  }|j                         st        j                  d|j                         |j                          E t        j                          |z   }| j                  D ]4  }t#        d|t        j                          z
        }|j                  |       6 | j                  D ]R  }|j                         r0t        j                  d|j                         |j%                          |j                          T | j                  |   }	t'        j(                  | j*                  |   t&        j,                        s|| j                  |   j                  }
|
dk  r@	 t/        j0                  |
       j2                  }t7        d
||fz  ||	j                  |
|      t7        d||
fz  ||	j                  |
      t9        | j*                  |   d      5 }t;        j<                  |      }ddd       d|z  }|z  }t?        |||	j                        # t4        $ r
 d|
  d	}Y w xY w# 1 sw Y   @xY w)a#  Join one or more processes within spawn context.

        Attempt to join one or more processes in this spawn context.
        If one of them exited with a non-zero exit status, this function
        kills the remaining processes and raises an exception with the cause
        of the first process exiting.

        Returns ``True`` if all processes have been joined successfully,
        ``False`` if there are more processes that need to be joined.

        Args:
            timeout (float): Wait this long before giving up on waiting.
        r   T)timeoutN   z)Terminating process %s via signal SIGTERMzJUnable to shutdown process %s via SIGTERM , forcefully exiting via SIGKILLz<Unknown signal >z$process %d terminated with signal %s)r   r   r-   r.   z'process %d terminated with exit code %d)r   r   r-   rbz5

-- Process %d terminated with the following error:
) lenrJ   multiprocessing
connectionwaitkeyspoprG   joinexitcodeis_alivelogwarningr   	terminatetime	monotonicmaxkillosaccessrF   R_OKr3   Signalsname
ValueErrorr   r8   r9   loadr   )r   rP   readyr   rI   rK   rL   endtime_to_waitfailed_processr[   rh   rB   original_tracer   s                  r   rZ   zProcessContext.joinq   s    t~~!#  **//NN! 0 

  	HNN&&x0EnnU+GLLN1$#	 t~~&!++ ~~ 	$G!GU!!#	$ nn(~~ 	'Gq#(8"89LLL&	' ~~ 	G!`KK LLN	 4yy))+6@~~k2;;H!|;!>>8)499D -:k4=PP +,00& $  -=h@WW +,00&	  $"";/6 	-"#[[_N	-H;V~$S+~7I7IJJ+ " ;-xi[:D;"	- 	-s   1 K& 'K<&K98K9<Lr   )r!   r"   r#   r   rN   rZ    r   r   r	   r	   f   s    
@ZKr   r	   c                        e Zd Z fdZ xZS )r   c                 P    t        j                  d       t        |   ||       y )Nz<SpawnContext is renamed to ProcessContext since 1.4 release.)warningswarnr   r   )r   rG   rF   r   s      r   r   zSpawnContext.__init__   s    TUK0r   )r!   r"   r#   r   r'   r(   s   @r   r   r      s    1 1r   r   c                     |dk(  r?t         j                  j                  t        d      dk(  rt        j                  d       d}nd}t        j                  |      d g|z  }d g|z  } fd}	|s't        |      D ]  }
 |	|
      \  }}}|||<   |||<    nlt        |      5 }t        |      D 
cg c]  }
|j                  |	|
       }}
t        |      D ]   }|j                         \  }}}|||<   |||<   " 	 d d d        t        ||      }|s|S |j                         s	 |j                         sy y c c}
w # 1 sw Y   BxY w)	N
forkserver01zStarting processes in parallel.TFc                    t        j                  ddd      }|j                          t        j                  |j
                         j                  t        | |j
                  f      }|j                          | ||j
                  fS )Nzpytorch-errorfile-z.pickleF)prefixsuffixdelete)targetr@   daemon)	tempfileNamedTemporaryFilecloserd   unlinkrh   ProcessrC   start)r?   tfrL   r@   r~   r>   mps      r   start_processz&start_processes.<locals>.start_process   s|     (('	%
 	

		"''**arww'  

 	'277""r   )max_workers)rd   environgetENV_VAR_PARALLEL_STARTr]   inforU   get_contextranger   submitr   resultr	   rZ   )r>   r@   nprocsrZ   r~   start_methodstart_parallelrF   rG   r   r?   idxrL   tf_nameexecutorfuturesfutcontextr   s   ``  `             @r   r   r      sV    	$JJNN1373>23 		$	$\	2B&6/KI#( v 	%A$1!$4!C'&K$IcN	%
  F3 	)xBG-PQx}a8PGP#G, )(+

%Wg#*C !(	#	)	) Y4G lln lln Q	) 	)s   )E7D=0E=EEc                 r    |dk7  r"d| d}t        j                  |t        d       t        | ||||d      S )a  Spawns ``nprocs`` processes that run ``fn`` with ``args``.

    If one of the processes exits with a non-zero exit status, the
    remaining processes are killed and an exception is raised with the
    cause of termination. In the case an exception was caught in the
    child process, it is forwarded and its traceback is included in
    the exception raised in the parent process.

    Args:
        fn (function): Function is called as the entrypoint of the
            spawned process. This function must be defined at the top
            level of a module so it can be pickled and spawned. This
            is a requirement imposed by multiprocessing.

            The function is called as ``fn(i, *args)``, where ``i`` is
            the process index and ``args`` is the passed through tuple
            of arguments.

        args (tuple): Arguments passed to ``fn``.
        nprocs (int): Number of processes to spawn.
        join (bool): Perform a blocking join on all processes.
        daemon (bool): The spawned processes' daemon flag. If set to True,
                       daemonic processes will be created.
        start_method (str): (deprecated) this method will always use ``spawn``
                               as the start method. To use a different start method
                               use ``start_processes()``.

    Returns:
        None if ``join`` is ``True``,
        :class:`~ProcessContext` if ``join`` is ``False``

    r   z3This method only supports start_method=spawn (got: zU).
To use a different start_method use:
		 torch.multiprocessing.start_processes(...)   )
stacklevel)r   )rs   rt   FutureWarningr   )r>   r@   r   rZ   r~   r   r   s          r   r   r      sK    B wA, P: : 	
 	c=Q72tVT6PPr   )rp   r   TFr   )loggingrU   multiprocessing.connectionrd   r9   r3   r<   r   r`   rs   concurrent.futuresr   r   typingr    r   r   	getLoggerr!   r]   __all__r6   r
   r   r   rC   r	   r   r   r   rp   r   r   <module>r      s      ! 	   
    ?  % 3 g!
By 
B	6- 	6
- 
0(eK eKP1> 1  
	AH(Qr   