U
    •Ê¹jÈ!  ã                   @   s®   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 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 )é    N)ÚOptionalé   )Ú_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__© úZ/var/www/html/bwave/python/venv/lib/python3.8/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   ;   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   sh   t tjƒ z| |f|žŽ  W nF tk
r0   Y n4 tk
rb   dd l}| | ¡ ¡ t 	d¡ Y nX d S )Nr   r   )
r   ÚsignalÚSIGINTÚKeyboardInterruptÚ	ExceptionÚ	tracebackÚputÚ
format_excÚsysÚexit)ÚfnÚiÚargsÚerror_queuer%   r   r   r   Ú_wrapB   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>Y   s     z+ProcessContext.__init__.<locals>.<dictcomp>)Úerror_queuesÚ	processesÚ	enumerateÚ	sentinels©r   r6   r5   r   r   r   r   V   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>^   s     z'ProcessContext.pids.<locals>.<listcomp>)r6   r   r   r   r   Úpids]   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kr|t | jƒdkS | jD ]}| 
¡ r–| ¡  | ¡  q‚| j| }| j|  ¡ �r| j| j	}|dk rút | ¡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_processrD   rI   Zoriginal_tracer   r   r   r   rC   `   sT    þ





û
üzProcessContext.join)N)r   r   r   r   r;   rC   r   r   r   r   r/   U   s   r/   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚSpawnContextc                    s   t  d¡ tƒ  ||¡ d S )Nz<SpawnContext is renamed to ProcessContext since 1.4 release.)ÚwarningsÚwarnr   r   r9   r   r   r   r   §   s    
zSpawnContext.__init__)r   r   r   r   r   r   r   r   r   rL   ¦   s   rL   r   TFÚspawnc                 C   s|   t  |¡}g }g }t|ƒD ]@}	| ¡ }
|jt| |	||
f|d�}| ¡  | |
¡ | |¡ qt||ƒ}|sn|S | 	¡ sxqnd S )N)Útargetr,   Údaemon)
r>   Zget_contextÚrangeÚSimpleQueueÚProcessr.   ÚstartÚappendr/   rC   )r*   r,   ÚnprocsrC   rQ   Ú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 (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``

    rO   zŠThis method only supports start_method=spawn (got: %s).
To use a different start_method use:
		 torch.multiprocessing.start_processes(...))rX   )rM   rN   r[   )r*   r,   rW   rC   rQ   rX   r   r   r   r   rO   Î   s    !þÿ
)r   r   TFrO   )r   r   TFrO   )r>   Zmultiprocessing.connectionr!   r(   rM   Útypingr   Ú r   r$   r   r   r   r.   r/   rL   r[   rO   r   r   r   r   Ú<module>   s&   Q         ÿ
