a
    8Sic!                     @   s   d dl m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 G dd de	Z
G dd de
ZG d	d
 d
e
Zdd ZG dd dZG dd deZdddZdddZdS )    )OptionalN   )_prctl_pr_set_pdeathsigc                       s6   e Zd ZddgZeeed fddZdd Z  ZS )ProcessExceptionerror_index	error_pid)msgr   pidc                    s"   t  | || _|| _|| _d S N)super__init__r   r   r	   )selfr   r   r	   	__class__ W/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/multiprocessing/spawn.pyr      s    zProcessException.__init__c                 C   s   t | | j| j| jffS r
   )typer   r   r	   r   r   r   r   
__reduce__   s    zProcessException.__reduce__)	__name__
__module____qualname__	__slots__strintr   r   __classcell__r   r   r   r   r      s   r   c                       s*   e Zd ZdZeeed fddZ  ZS )ProcessRaisedExceptionz^
    Exception is thrown when the process failed due to exception
    raised by the code.
    )r   r   r   c                    s   t  ||| d S r
   )r   r   )r   r   r   r   r   r   r   r      s    zProcessRaisedException.__init__)r   r   r   __doc__r   r   r   r   r   r   r   r   r      s
   r   c                       sB   e Zd ZdZdgZd	eeeeee d fddZdd Z	  Z
S )
ProcessExitedExceptionzg
    Exception is thrown when the process failed due to signal
    or exited with a specific code.
    	exit_codeN)r   r   r   r   signal_namec                    s    t  ||| || _|| _d S r
   )r   r   r   r    )r   r   r   r   r   r    r   r   r   r   .   s    zProcessExitedException.__init__c                 C   s    t | | j| j| j| j| jffS r
   )r   r   r   r	   r   r    r   r   r   r   r   6   s    z!ProcessExitedException.__reduce__)N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   '   s    r   c                 C   sf   t tj z| |g|R   W nB ty0   Y n2 ty`   dd l}||  t	d Y n0 d S )Nr   r   )
r   signalSIGINTKeyboardInterrupt	Exception	tracebackput
format_excsysexit)fniargserror_queuer%   r   r   r   _wrap=   s    
r.   c                   @   s&   e Zd Zdd Zdd ZdddZdS )	ProcessContextc                 C   s$   || _ || _dd t|D | _d S )Nc                 S   s   i | ]\}}|j |qS r   )sentinel).0indexprocessr   r   r   
<dictcomp>S   s   z+ProcessContext.__init__.<locals>.<dictcomp>)error_queues	processes	enumerate	sentinelsr   r6   r5   r   r   r   r   P   s
    zProcessContext.__init__c                 C   s   dd | j D S )Nc                 S   s   g | ]}t |jqS r   )r   r	   )r1   r3   r   r   r   
<listcomp>Y       z'ProcessContext.pids.<locals>.<listcomp>)r6   r   r   r   r   pidsX   s    zProcessContext.pidsNc                 C   sD  t | jdkrdS tjj| j |d}d}|D ]4}| j|}| j| }|  |j	dkr0|} qfq0|du r|t | jdkS | jD ]}|
 r|  |  q| j| }| j|  r| j| j	}|dk rt| j}	td||	f ||j||	dntd||f ||j|d| j|  }
d	| }||
7 }t|||jdS )
a  
        Tries 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)timeoutNz$process %d terminated with signal %s)r   r   r   r    z'process %d terminated with exit code %d)r   r   r   z5

-- Process %d terminated with the following error:
)lenr8   multiprocessing
connectionwaitkeyspopr6   joinexitcodeis_alive	terminater5   emptyr!   Signalsnamer   r	   getr   )r   r=   readyr   r0   r2   r3   Zfailed_processrE   rJ   Zoriginal_tracer   r   r   r   rD   [   s\    




	zProcessContext.join)N)r   r   r   r   r<   rD   r   r   r   r   r/   O   s   r/   c                       s   e Zd Z fddZ  ZS )SpawnContextc                    s    t d tt| || d S )Nz<SpawnContext is renamed to ProcessContext since 1.4 release.)warningswarnr   rM   r   r9   r   r   r   r      s    
zSpawnContext.__init__)r   r   r   r   r   r   r   r   r   rM      s   rM   r   TFspawnc                 C   s|   t |}g }g }t|D ]@}	| }
|jt| |	||
f|d}|  ||
 || qt||}|sn|S |	 sxqnd S )N)targetr,   daemon)
r?   get_contextrangeSimpleQueueProcessr.   startappendr/   rD   )r*   r,   nprocsrD   rR   start_methodmpr5   r6   r+   r-   r3   contextr   r   r   start_processes   s$    



r]   c                 C   s.   |dkrd| }t | t| ||||d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 (string): (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``

    rP   zThis method only supports start_method=spawn (got: %s).
To use a different start_method use:
		 torch.multiprocessing.start_processes(...))rZ   )rN   rO   r]   )r*   r,   rY   rD   rR   rZ   r   r   r   r   rP      s    !
)r   r   TFrP   )r   r   TFrP   )typingr   r?   multiprocessing.connectionr!   r(   rN    r   r$   r   r   r   r.   r/   rM   r]   rP   r   r   r   r   <module>   s   T
