U
    ad+                     @   s  d dl Z d dlZd dlZddlmZ ddlmZ dZG dd deZG dd	 d	eZ	G d
d deZ
G dd deZG dd deZG dd dejZG dd deZejdkrRG dd dejZG dd dejZG dd dejZG dd deZG dd deZG dd  d eZe e e d!Zejd"krDeed# Zneed$ Zn8G d%d dejZG d&d deZd#e iZeed# Zd'd( Ze Zd)d* Zd+d, Zd-d. ZdS )/    N   )process)	reduction c                   @   s   e Zd ZdS )ProcessErrorN__name__
__module____qualname__r   r   r   -/usr/lib/python3.8/multiprocessing/context.pyr      s   r   c                   @   s   e Zd ZdS )BufferTooShortNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )TimeoutErrorNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )AuthenticationErrorNr   r   r   r   r   r      s   r   c                   @   sX  e Zd ZeZeZeZeZeej	Z	eej
Z
eejZdd Zdd ZdCddZdd	 Zd
d ZdDddZdEddZdFddZdd ZdGddZdHddZdIddZdd ZdJd d!Zd"d# Zd$d% Zdd&d'd(Zdd&d)d*Zd+d, Zd-d. ZdKd/d0Z d1d2 Z!d3d4 Z"d5d6 Z#dLd7d8Z$dMd:d;Z%dNd<d=Z&e'd>d? Z(e(j)d@d? Z(dAdB Z*dS )OBaseContextc                 C   s"   t  }|dkrtdn|S dS )z(Returns the number of CPUs in the systemNzcannot determine number of cpus)os	cpu_countNotImplementedError)selfZnumr   r   r   r   )   s    
zBaseContext.cpu_countc                 C   s&   ddl m} ||  d}|  |S )zReturns a manager associated with a running server process

        The managers methods such as `Lock()`, `Condition()` and `Queue()`
        can be used to create shared objects.
        r   )SyncManagerctx)Zmanagersr   get_contextstart)r   r   mr   r   r   Manager1   s    zBaseContext.ManagerTc                 C   s   ddl m} ||S )z1Returns two connection object connected by a piper   )Pipe)
connectionr   )r   Zduplexr   r   r   r   r   <   s    zBaseContext.Pipec                 C   s   ddl m} ||  dS )z#Returns a non-recursive lock objectr   )Lockr   )synchronizer   r   )r   r   r   r   r   r   A   s    zBaseContext.Lockc                 C   s   ddl m} ||  dS )zReturns a recursive lock objectr   )RLockr   )r   r   r   )r   r   r   r   r   r   F   s    zBaseContext.RLockNc                 C   s   ddl m} |||  dS )zReturns a condition objectr   )	Conditionr   )r   r    r   )r   lockr    r   r   r   r    K   s    zBaseContext.Conditionr   c                 C   s   ddl m} |||  dS )zReturns a semaphore objectr   )	Semaphorer   )r   r"   r   )r   valuer"   r   r   r   r"   P   s    zBaseContext.Semaphorec                 C   s   ddl m} |||  dS )z"Returns a bounded semaphore objectr   )BoundedSemaphorer   )r   r$   r   )r   r#   r$   r   r   r   r$   U   s    zBaseContext.BoundedSemaphorec                 C   s   ddl m} ||  dS )zReturns an event objectr   )Eventr   )r   r%   r   )r   r%   r   r   r   r%   Z   s    zBaseContext.Eventc                 C   s    ddl m} |||||  dS )zReturns a barrier objectr   )Barrierr   )r   r&   r   )r   ZpartiesactionZtimeoutr&   r   r   r   r&   _   s    zBaseContext.Barrierr   c                 C   s   ddl m} |||  dS )Returns a queue objectr   )Queuer   )queuesr)   r   )r   maxsizer)   r   r   r   r)   d   s    zBaseContext.Queuec                 C   s   ddl m} |||  dS )r(   r   )JoinableQueuer   )r*   r,   r   )r   r+   r,   r   r   r   r,   i   s    zBaseContext.JoinableQueuec                 C   s   ddl m} ||  dS )r(   r   )SimpleQueuer   )r*   r-   r   )r   r-   r   r   r   r-   n   s    zBaseContext.SimpleQueuer   c                 C   s"   ddl m} ||||||  dS )zReturns a process pool objectr   )Pool)context)Zpoolr.   r   )r   Z	processesZinitializerZinitargsZmaxtasksperchildr.   r   r   r   r.   s   s    
zBaseContext.Poolc                 G   s   ddl m} ||f| S )zReturns a shared objectr   )RawValue)sharedctypesr0   )r   typecode_or_typeargsr0   r   r   r   r0   z   s    zBaseContext.RawValuec                 C   s   ddl m} |||S )zReturns a shared arrayr   )RawArray)r1   r4   )r   r2   size_or_initializerr4   r   r   r   r4      s    zBaseContext.RawArray)r!   c                G   s&   ddl m} ||f|||  dS )z$Returns a synchronized shared objectr   )Valuer!   r   )r1   r6   r   )r   r2   r!   r3   r6   r   r   r   r6      s    zBaseContext.Valuec                C   s    ddl m} |||||  dS )z#Returns a synchronized shared arrayr   )Arrayr7   )r1   r8   r   )r   r2   r5   r!   r8   r   r   r   r8      s    zBaseContext.Arrayc                 C   s,   t jdkr(tt ddr(ddlm} |  dS )zCheck whether this is a fake forked process in a frozen executable.
        If so then run code specified by commandline and exit.
        win32frozenFr   )freeze_supportN)sysplatformgetattrspawnr;   )r   r;   r   r   r   r;      s    zBaseContext.freeze_supportc                 C   s   ddl m} | S )zZReturn package logger -- if it does not already exist then
        it is created.
        r   )
get_logger)utilr@   )r   r@   r   r   r   r@      s    zBaseContext.get_loggerc                 C   s   ddl m} ||S )z8Turn on logging and add a handler which prints to stderrr   )log_to_stderr)rA   rB   )r   levelrB   r   r   r   rB      s    zBaseContext.log_to_stderrc                 C   s   ddl m} dS )zVInstall support for sending connections and sockets
        between processes
        r   )r   N) r   )r   r   r   r   r   allow_connection_pickling   s    z%BaseContext.allow_connection_picklingc                 C   s   ddl m} || dS )zSets the path to a python.exe or pythonw.exe binary used to run
        child processes instead of sys.executable when using the 'spawn'
        start method.  Useful for people embedding Python.
        r   )set_executableN)r?   rF   )r   
executablerF   r   r   r   rF      s    zBaseContext.set_executablec                 C   s   ddl m} || dS )zkSet list of module names to try to load in forkserver process.
        This is really just a hint.
        r   )set_forkserver_preloadN)
forkserverrH   )r   Zmodule_namesrH   r   r   r   rH      s    z"BaseContext.set_forkserver_preloadc                 C   sH   |d kr| S zt | }W n" tk
r:   td| d Y nX |  |S )Nzcannot find context for %r)_concrete_contextsKeyError
ValueError_check_available)r   methodr   r   r   r   r      s    zBaseContext.get_contextFc                 C   s   | j S N)_namer   Z
allow_noner   r   r   get_start_method   s    zBaseContext.get_start_methodc                 C   s   t dd S )Nz+cannot set start method of concrete context)rL   r   rN   Zforcer   r   r   set_start_method   s    zBaseContext.set_start_methodc                 C   s   t  dS )z_Controls how objects will be reduced to a form that can be
        shared with other processes.r   )globalsgetr   r   r   r   reducer   s    zBaseContext.reducerc                 C   s   |t  d< d S )Nr   )rU   )r   r   r   r   r   rX      s    c                 C   s   d S rO   r   rW   r   r   r   rM      s    zBaseContext._check_available)T)N)r   )r   )NN)r   )r   )NNr   N)N)N)F)F)+r   r	   r
   r   r   r   r   staticmethodr   Zcurrent_processZparent_processZactive_childrenr   r   r   r   r   r    r"   r$   r%   r&   r)   r,   r-   r.   r0   r4   r6   r8   r;   r@   rB   rE   rF   rH   r   rR   rT   propertyrX   setterrM   r   r   r   r   r      sR   









  







r   c                   @   s   e Zd ZdZedd ZdS )ProcessNc                 C   s   t  j| S rO   )_default_contextr   r\   _Popen)process_objr   r   r   r^      s    zProcess._Popenr   r	   r
   Z_start_methodrY   r^   r   r   r   r   r\      s   r\   c                       sF   e Zd ZeZdd Zd fdd	ZdddZdd	d
Zdd Z  Z	S )DefaultContextc                 C   s   || _ d | _d S rO   )r]   _actual_context)r   r/   r   r   r   __init__   s    zDefaultContext.__init__Nc                    s0   |d kr | j d kr| j| _ | j S t |S d S rO   )rb   r]   superr   )r   rN   	__class__r   r   r      s
    
zDefaultContext.get_contextFc                 C   s<   | j d k	r|std|d kr,|r,d | _ d S | || _ d S )Nzcontext has already been set)rb   RuntimeErrorr   rS   r   r   r   rT      s    zDefaultContext.set_start_methodc                 C   s"   | j d kr|rd S | j| _ | j jS rO   )rb   r]   rP   rQ   r   r   r   rR      s
    
zDefaultContext.get_start_methodc                 C   sB   t jdkrdgS t jdkr"ddgnddg}tjr:|d |S d S )Nr9   r?   darwinforkrI   )r<   r=   r   HAVE_SEND_HANDLEappend)r   methodsr   r   r   get_all_start_methods   s    

z$DefaultContext.get_all_start_methods)N)F)F)
r   r	   r
   r\   rc   r   rT   rR   rm   __classcell__r   r   re   r   ra      s   

ra   r9   c                   @   s   e Zd ZdZedd ZdS )ForkProcessri   c                 C   s   ddl m} || S Nr   )Popen)Z
popen_forkrq   r_   rq   r   r   r   r^     s    zForkProcess._PopenNr`   r   r   r   r   ro     s   ro   c                   @   s   e Zd ZdZedd ZdS )SpawnProcessr?   c                 C   s   ddl m} || S rp   )Zpopen_spawn_posixrq   rr   r   r   r   r^     s    SpawnProcess._PopenNr`   r   r   r   r   rs     s   rs   c                   @   s   e Zd ZdZedd ZdS )ForkServerProcessrI   c                 C   s   ddl m} || S rp   )Zpopen_forkserverrq   rr   r   r   r   r^      s    zForkServerProcess._PopenNr`   r   r   r   r   ru     s   ru   c                   @   s   e Zd ZdZeZdS )ForkContextri   N)r   r	   r
   rP   ro   r\   r   r   r   r   rv   %  s   rv   c                   @   s   e Zd ZdZeZdS SpawnContextr?   Nr   r	   r
   rP   rs   r\   r   r   r   r   rx   )  s   rx   c                   @   s   e Zd ZdZeZdd ZdS )ForkServerContextrI   c                 C   s   t jstdd S )Nz%forkserver start method not available)r   rj   rL   rW   r   r   r   rM   0  s    z"ForkServerContext._check_availableN)r   r	   r
   rP   ru   r\   rM   r   r   r   r   rz   -  s   rz   )ri   r?   rI   rh   r?   ri   c                   @   s   e Zd ZdZedd ZdS )rs   r?   c                 C   s   ddl m} || S rp   )Zpopen_spawn_win32rq   rr   r   r   r   r^   D  s    rt   Nr`   r   r   r   r   rs   B  s   c                   @   s   e Zd ZdZeZdS rw   ry   r   r   r   r   rx   I  s   c                 C   s   t |  t_d S rO   )rJ   r]   rb   )rN   r   r   r   _force_start_methodV  s    r{   c                   C   s   t tdd S )Nspawning_popen)r>   _tlsr   r   r   r   get_spawning_popen_  s    r~   c                 C   s
   | t _d S rO   )r}   r|   )popenr   r   r   set_spawning_popenb  s    r   c                 C   s    t  d krtdt| j d S )NzF%s objects should only be shared between processes through inheritance)r~   rg   typer   )objr   r   r   assert_spawninge  s    
r   ) r   r<   Z	threadingrD   r   r   __all__	Exceptionr   r   r   r   objectr   ZBaseProcessr\   ra   r=   ro   rs   ru   rv   rx   rz   rJ   r]   r{   Zlocalr}   r~   r   r   r   r   r   r   <module>   sL    ?, 