o
    eZ-                     @   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rG 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reed# Zn#eed# ZnG 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                   @      e Zd ZdS )ProcessErrorN__name__
__module____qualname__r   r   r   DD:\Projects\ConvertPro\env\Lib\site-packages\multiprocess/context.pyr          r   c                   @   r   )BufferTooShortNr   r   r   r   r   r      r   r   c                   @   r   )TimeoutErrorNr   r   r   r   r   r      r   r   c                   @   r   )AuthenticationErrorNr   r   r   r   r   r      r   r   c                   @   s\  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dEddZdd ZdFddZdGddZdGddZdd Z		dHd d!Zd"d# Zd$d% Zdd&d'd(Zdd&d)d*Zd+d, Zd-d. ZdDd/d0Z d1d2 Z!d3d4 Z"d5d6 Z#dDd7d8Z$dId:d;Z%dId<d=Z&e'd>d? Z(e(j)d@d? Z(dAdB Z*dS )JBaseContextc                 C   s   t  }|du rtd|S )z(Returns the number of CPUs in the systemNzcannot determine number of cpus)os	cpu_countNotImplementedError)self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      ddl m} ||S )z1Returns two connection object connected by a piper   )Pipe)
connectionr   )r   duplexr   r   r   r   r   <      zBaseContext.Pipec                 C      ddl m} ||  dS )z#Returns a non-recursive lock objectr   )Lockr   )synchronizer$   r   )r   r$   r   r   r   r$   A      zBaseContext.Lockc                 C   r#   )zReturns a recursive lock objectr   )RLockr   )r%   r'   r   )r   r'   r   r   r   r'   F   r&   zBaseContext.RLockNc                 C      ddl m} |||  dS )zReturns a condition objectr   )	Conditionr   )r%   r)   r   )r   lockr)   r   r   r   r)   K      zBaseContext.Conditionr   c                 C   r(   )zReturns a semaphore objectr   )	Semaphorer   )r%   r,   r   )r   valuer,   r   r   r   r,   P   r+   zBaseContext.Semaphorec                 C   r(   )z"Returns a bounded semaphore objectr   )BoundedSemaphorer   )r%   r.   r   )r   r-   r.   r   r   r   r.   U   r+   zBaseContext.BoundedSemaphorec                 C   r#   )zReturns an event objectr   )Eventr   )r%   r/   r   )r   r/   r   r   r   r/   Z   r&   zBaseContext.Eventc                 C       ddl m} |||||  dS )zReturns a barrier objectr   )Barrierr   )r%   r1   r   )r   partiesactiontimeoutr1   r   r   r   r1   _   s   zBaseContext.Barrierr   c                 C   r(   )Returns a queue objectr   )Queuer   )queuesr6   r   )r   maxsizer6   r   r   r   r6   d   r+   zBaseContext.Queuec                 C   r(   )r5   r   )JoinableQueuer   )r7   r9   r   )r   r8   r9   r   r   r   r9   i   r+   zBaseContext.JoinableQueuec                 C   r#   )r5   r   )SimpleQueuer   )r7   r:   r   )r   r:   r   r   r   r:   n   r&   zBaseContext.SimpleQueuer   c                 C   s"   ddl m} ||||||  dS )zReturns a process pool objectr   )Pool)context)poolr;   r   )r   Z	processesZinitializerZinitargsZmaxtasksperchildr;   r   r   r   r;   s   s   
zBaseContext.Poolc                 G   s   ddl m} ||g|R  S )zReturns a shared objectr   )RawValue)sharedctypesr>   )r   typecode_or_typeargsr>   r   r   r   r>   z   r+   zBaseContext.RawValuec                 C   s   ddl m} |||S )zReturns a shared arrayr   )RawArray)r?   rB   )r   r@   size_or_initializerrB   r   r   r   rB      s   
zBaseContext.RawArray)r*   c                G   s(   ddl m} ||g|R ||  dS )z$Returns a synchronized shared objectr   )Valuer*   r   )r?   rD   r   )r   r@   r*   rA   rD   r   r   r   rD      s   zBaseContext.Valuec                C   r0   )z#Returns a synchronized shared arrayr   )ArrayrE   )r?   rF   r   )r   r@   rC   r*   rF   r   r   r   rF      s   zBaseContext.Arrayc                 C   s4   t jdkrtt ddrddlm} |  dS dS 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spawnrI   )r   rI   r   r   r   rI      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)utilrN   )r   rN   r   r   r   rN      s   zBaseContext.get_loggerc                 C   r   )z8Turn on logging and add a handler which prints to stderrr   )log_to_stderr)rO   rP   )r   levelrP   r   r   r   rP      r"   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      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)rM   rU   )r   
executablerU   r   r   r   rU      s   zBaseContext.set_executablec                 C   rT   )zkSet list of module names to try to load in forkserver process.
        This is really just a hint.
        r   )set_forkserver_preloadN)
forkserverrW   )r   Zmodule_namesrW   r   r   r   rW      s   z"BaseContext.set_forkserver_preloadc                 C   sB   |d u r| S zt | }W n ty   td| d w |  |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   
allow_noner   r   r   get_start_method   s   zBaseContext.get_start_methodc                 C   s   t d)Nz+cannot set start method of concrete context)r[   r   r]   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   )rf   )r   r   r   r   r   ri         c                 C      d S r^   r   rh   r   r   r   r\      s   zBaseContext._check_available)Tr^   )r   )NN)r   )NNr   NF)+r	   r
   r   r   r   r   r   staticmethodr   current_processZparent_processZactive_childrenr   r   r   r$   r'   r)   r,   r.   r/   r1   r6   r9   r:   r;   r>   rB   rD   rF   rI   rN   rP   rS   rU   rW   r   rb   re   propertyri   setterr\   r   r   r   r   r      sT    

















r   c                   @   s(   e Zd ZdZedd Zedd ZdS )ProcessNc                 C   s   t  j| S r^   )_default_contextr   rq   _Popen)process_objr   r   r   rs      s   zProcess._Popenc                   C   s   t  j S r^   )rr   r   rq   _after_forkr   r   r   r   ru      rj   zProcess._after_forkr	   r
   r   _start_methodrm   rs   ru   r   r   r   r   rq      s    
rq   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 r^   )rr   _actual_context)r   r<   r   r   r   __init__   s   
zDefaultContext.__init__Nc                    s,   |d u r| j d u r| j| _ | j S t |S r^   )ry   rr   superr   )r   r]   	__class__r   r   r      s
   
zDefaultContext.get_contextFc                 C   s<   | j d ur|std|d u r|rd | _ d S | || _ d S )Nzcontext has already been set)ry   RuntimeErrorr   rc   r   r   r   re      s   zDefaultContext.set_start_methodc                 C   s"   | j d u r|r	d S | j| _ | j jS r^   )ry   rr   r_   r`   r   r   r   rb      s
   
zDefaultContext.get_start_methodc                 C   s>   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 )NrG   rM   darwinforkrX   )rJ   rK   r   HAVE_SEND_HANDLEappend)r   methodsr   r   r   get_all_start_methods  s   

z$DefaultContext.get_all_start_methodsr^   rl   )
r	   r
   r   rq   rz   r   re   rb   r   __classcell__r   r   r|   r   rx      s    

rx   rG   c                   @      e Zd ZdZedd ZdS )ForkProcessr   c                 C      ddl m} || S Nr   )Popen)Z
popen_forkr   rt   r   r   r   r   rs     r"   zForkProcess._PopenNr	   r
   r   rw   rm   rs   r   r   r   r   r         r   c                   @   (   e Zd ZdZedd Zedd ZdS )SpawnProcessrM   c                 C   r   r   )Zpopen_spawn_posixr   r   r   r   r   rs     r"   SpawnProcess._Popenc                   C   rk   r^   r   r   r   r   r   ru   "     SpawnProcess._after_forkNrv   r   r   r   r   r         
r   c                   @   r   )ForkServerProcessrX   c                 C   r   r   )Zpopen_forkserverr   r   r   r   r   rs   )  r"   zForkServerProcess._PopenNr   r   r   r   r   r   '  r   r   c                   @      e Zd ZdZeZdS )ForkContextr   N)r	   r
   r   r_   r   rq   r   r   r   r   r   .      r   c                   @   r   SpawnContextrM   Nr	   r
   r   r_   r   rq   r   r   r   r   r   2  r   r   c                   @   s   e Zd ZdZeZdd ZdS )ForkServerContextrX   c                 C   s   t jstdd S )Nz%forkserver start method not available)r   r   r[   rh   r   r   r   r\   9  s   z"ForkServerContext._check_availableN)r	   r
   r   r_   r   rq   r\   r   r   r   r   r   6  s    r   )r   rM   rX   r   r   c                   @   r   )r   rM   c                 C   r   r   )Zpopen_spawn_win32r   r   r   r   r   rs   M  r"   r   c                   C   rk   r^   r   r   r   r   r   ru   R  r   r   Nrv   r   r   r   r   r   K  r   c                   @   r   r   r   r   r   r   r   r   W  r   rM   c                 C   s   t |  t_d S r^   )rY   rr   ry   )r]   r   r   r   _force_start_methodd  s   r   c                   C   s   t tdd S )Nspawning_popen)rL   _tlsr   r   r   r   get_spawning_popenm  s   r   c                 C   s
   | t _d S r^   )r   r   )popenr   r   r   set_spawning_popenp  s   
r   c                 C   s    t  d u rtdt| j d S )NzF%s objects should only be shared between processes through inheritance)r   r~   typer	   )objr   r   r   assert_spawnings  s   
r   ) r   rJ   	threadingrR   r   r   __all__	Exceptionr   r   r   r   objectr   ZBaseProcessrq   rx   rK   r   r   r   r   r   r   rY   rr   r   localr   r   r   r   r   r   r   r   <module>   sL     ?

,
