o
    JAfM-                     @   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   ./usr/lib/python3.10/multiprocessing/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 )Nz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 )Nr   )SyncManagerctx)managersr   get_contextstart)r   r   mr   r   r   Manager1   s
   zBaseContext.ManagerTc                 C      	 ddl m} ||S )Nr   )Pipe)
connectionr    )r   duplexr    r   r   r   r    <      zBaseContext.Pipec                 C      	 ddl m} ||  dS )Nr   )Lockr   )synchronizer%   r   )r   r%   r   r   r   r%   A      zBaseContext.Lockc                 C   r$   )Nr   )RLockr   )r&   r(   r   )r   r(   r   r   r   r(   F   r'   zBaseContext.RLockNc                 C      	 ddl m} |||  dS )Nr   )	Conditionr   )r&   r*   r   )r   lockr*   r   r   r   r*   K      zBaseContext.Conditionr   c                 C   r)   )Nr   )	Semaphorer   )r&   r-   r   )r   valuer-   r   r   r   r-   P   r,   zBaseContext.Semaphorec                 C   r)   )Nr   )BoundedSemaphorer   )r&   r/   r   )r   r.   r/   r   r   r   r/   U   r,   zBaseContext.BoundedSemaphorec                 C   r$   )Nr   )Eventr   )r&   r0   r   )r   r0   r   r   r   r0   Z   r'   zBaseContext.Eventc                 C   "   	 ddl m} |||||  dS )Nr   )Barrierr   )r&   r2   r   )r   partiesactiontimeoutr2   r   r   r   r2   _   s   zBaseContext.Barrierr   c                 C   r)   )Nr   )Queuer   )queuesr6   r   )r   maxsizer6   r   r   r   r6   d   r,   zBaseContext.Queuec                 C   r)   )Nr   )JoinableQueuer   )r7   r9   r   )r   r8   r9   r   r   r   r9   i   r,   zBaseContext.JoinableQueuec                 C   r$   )Nr   )SimpleQueuer   )r7   r:   r   )r   r:   r   r   r   r:   n   r'   zBaseContext.SimpleQueuer   c                 C   s$   	 ddl m} ||||||  dS )Nr   )Pool)context)poolr;   r   )r   	processesinitializerinitargsmaxtasksperchildr;   r   r   r   r;   s   s
   
zBaseContext.Poolc                 G   s   	 ddl m} ||g|R  S )Nr   )RawValue)sharedctypesrB   )r   typecode_or_typeargsrB   r   r   r   rB   z   r,   zBaseContext.RawValuec                 C   s   	 ddl m} |||S )Nr   )RawArray)rC   rF   )r   rD   size_or_initializerrF   r   r   r   rF      s   
zBaseContext.RawArray)r+   c                G   s*   	 ddl m} ||g|R ||  dS )Nr   )Valuer+   r   )rC   rH   r   )r   rD   r+   rE   rH   r   r   r   rH      s
   zBaseContext.Valuec                C   r1   )Nr   )ArrayrI   )rC   rJ   r   )r   rD   rG   r+   rJ   r   r   r   rJ      s
   zBaseContext.Arrayc                 C   s6   	 t jdkrtt ddrddlm} |  d S d S d S )Nwin32frozenFr   )freeze_support)sysplatformgetattrspawnrM   )r   rM   r   r   r   rM      s
   
zBaseContext.freeze_supportc                 C   s   	 ddl m} | S )Nr   )
get_logger)utilrR   )r   rR   r   r   r   rR      s   zBaseContext.get_loggerc                 C   r   )Nr   )log_to_stderr)rS   rT   )r   levelrT   r   r   r   rT      r#   zBaseContext.log_to_stderrc                 C   s   	 ddl m} d S )Nr   )r!   ) r!   )r   r!   r   r   r   allow_connection_pickling   s   z%BaseContext.allow_connection_picklingc                 C      	 ddl m} || d S )Nr   )set_executable)rQ   rY   )r   
executablerY   r   r   r   rY      s   zBaseContext.set_executablec                 C   rX   )Nr   )set_forkserver_preload)
forkserverr[   )r   module_namesr[   r   r   r   r[      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   rb   forcer   r   r   set_start_method   s   zBaseContext.set_start_methodc                 C   s   	 t  dS Nr   )globalsgetr   r   r   r   reducer   s   zBaseContext.reducerc                 C   s   |t  d< d S rk   )rl   )r   r   r   r   r   ro         c                 C      d S rc   r   rn   r   r   r   ra      s   zBaseContext._check_available)Trc   )r   )NN)r   )NNr   NF)+r	   r
   r   r   r   r   r   staticmethodr   current_processparent_processactive_childrenr   r   r    r%   r(   r*   r-   r/   r0   r2   r6   r9   r:   r;   rB   rF   rH   rJ   rM   rR   rT   rW   rY   r[   r   rg   rj   propertyro   setterra   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 rc   )_default_contextr   ry   _Popen)process_objr   r   r   r{      s   zProcess._Popenc                   C   s   t  j S rc   )rz   r   ry   _after_forkr   r   r   r   r}      rp   zProcess._after_forkr	   r
   r   _start_methodrs   r{   r}   r   r   r   r   ry      s    
ry   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 rc   )rz   _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 rc   )r   rz   superr   )r   rb   	__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)r   RuntimeErrorr   rh   r   r   r   rj      s   zDefaultContext.set_start_methodc                 C   s"   | j d u r|r	d S | j| _ | j jS rc   )r   rz   rd   re   r   r   r   rg      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 )NrK   rQ   darwinforkr\   )rN   rO   r   HAVE_SEND_HANDLEappend)r   methodsr   r   r   get_all_start_methods  s   

z$DefaultContext.get_all_start_methodsrc   rr   )
r	   r
   r   ry   r   r   rj   rg   r   __classcell__r   r   r   r   r      s    

r   rK   c                   @      e Zd ZdZedd ZdS )ForkProcessr   c                 C      ddl m} || S Nr   )Popen)
popen_forkr   r|   r   r   r   r   r{        zForkProcess._PopenNr	   r
   r   r   rs   r{   r   r   r   r   r         r   c                   @   (   e Zd ZdZedd Zedd ZdS )SpawnProcessrQ   c                 C   r   r   )popen_spawn_posixr   r   r   r   r   r{     r   SpawnProcess._Popenc                   C   rq   rc   r   r   r   r   r   r}   "     SpawnProcess._after_forkNr~   r   r   r   r   r         
r   c                   @   r   )ForkServerProcessr\   c                 C   r   r   )popen_forkserverr   r   r   r   r   r{   )  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   rd   r   ry   r   r   r   r   r   .      r   c                   @   r   SpawnContextrQ   Nr	   r
   r   rd   r   ry   r   r   r   r   r   2  r   r   c                   @   s   e Zd ZdZeZdd ZdS )ForkServerContextr\   c                 C   s   t jstdd S )Nz%forkserver start method not available)r   r   r`   rn   r   r   r   ra   9  s   z"ForkServerContext._check_availableN)r	   r
   r   rd   r   ry   ra   r   r   r   r   r   6  s    r   )r   rQ   r\   r   rQ   r   c                   @   r   )r   rQ   c                 C   r   r   )Zpopen_spawn_win32r   r   r   r   r   r{   M  r   r   c                   C   rq   rc   r   r   r   r   r   r}   R  r   r   Nr~   r   r   r   r   r   K  r   c                   @   r   r   r   r   r   r   r   r   W  r   c                 C   s   t |  t_d S rc   )r^   rz   r   )rb   r   r   r   _force_start_methodd  s   r   c                   C   s   t tdd S )Nspawning_popen)rP   _tlsr   r   r   r   get_spawning_popenm  s   r   c                 C   s
   | t _d S rc   )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   rN   	threadingrV   r   r   __all__	Exceptionr   r   r   r   objectr   BaseProcessry   r   rO   r   r   r   r   r   r   r^   rz   r   localr   r   r   r   r   r   r   r   <module>   sL     ?

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