o
    Dfb3                     @   s  d dl Z d dlZd dlZd dlZddlmZ g ZdZddlm	Z	m
Z
mZmZmZmZmZ G dd dZG dd	 d	ejZG d
d deZedd eeD e_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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   )processzIforce_execv is not supported as the billiard C extension is not installed)ProcessErrorBufferTooShortTimeoutErrorAuthenticationErrorTimeLimitExceededSoftTimeLimitExceededWorkerLostErrorc                   @   sd  e Zd ZeZeZeZeZeZeZe	Z	e
ejZe
ejZeedr&dd Zndd Zdd ZdEd	d
Zdd Zdd ZdFddZdGddZdGddZdd ZdHddZdIddZdIddZd d! Z	"					dJd#d$Zd%d& Zd'd( Zd)d* Z d+d, Z!d-d. Z"d/d0 Z#dFd1d2Z$d3d4 Z%d5d6 Z&d7d8 Z'dFd9d:Z(dKd;d<Z)dFd=d>Z*d?d@ Z+dAdB Z,dCdD Z-dS )LBaseContext	cpu_countc                 C   s   t  }|du rtd|S )z(Returns the number of CPUs in the systemNcannot determine number of cpus)osr   NotImplementedError)selfnum r   I/home/ubuntu/webapp/venv/lib/python3.10/site-packages/billiard/context.pyr   2   s   zBaseContext.cpu_countc              
   C   s   t jdkrz	ttjd }W na ttfy   d}Y nUw dt jv s&t jdkrZd}t jdkr1d| }zt|}t| }W d    n1 sHw   Y  W n! tyY   d}Y nw zt	d}W n tt
tfyo   d}Y nw |d	krv|S td
)Nwin32NUMBER_OF_PROCESSORSr   bsddarwinz/sbin/sysctl -n hw.ncpuz/usrSC_NPROCESSORS_ONLNr   r   )sysplatformintr   environ
ValueErrorKeyErrorpopenreadsysconfOSErrorAttributeErrorr   )r   r   commpr   r   r   r   :   s4   

c                 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)managersr&   get_contextstart)r   r&   mr   r   r   ManagerT   s   zBaseContext.ManagerTFc                 C   s   ddl m} ||||S )z1Returns two connection object connected by a piper   )Pipe)
connectionr.   )r   duplex	rnonblock	wnonblockr.   r   r   r   r.   _   s   zBaseContext.Pipec                 C      ddl m} ||  dS )z#Returns a non-recursive lock objectr   )Lockr'   )synchronizer4   r*   )r   r4   r   r   r   r4   d      zBaseContext.Lockc                 C   r3   )zReturns a recursive lock objectr   )RLockr'   )r5   r7   r*   )r   r7   r   r   r   r7   i   r6   zBaseContext.RLockNc                 C      ddl m} |||  dS )zReturns a condition objectr   )	Conditionr'   )r5   r9   r*   )r   lockr9   r   r   r   r9   n      zBaseContext.Conditionr   c                 C   r8   )zReturns a semaphore objectr   )	Semaphorer'   )r5   r<   r*   )r   valuer<   r   r   r   r<   s   r;   zBaseContext.Semaphorec                 C   r8   )z"Returns a bounded semaphore objectr   )BoundedSemaphorer'   )r5   r>   r*   )r   r=   r>   r   r   r   r>   x   r;   zBaseContext.BoundedSemaphorec                 C   r3   )zReturns an event objectr   )Eventr'   )r5   r?   r*   )r   r?   r   r   r   r?   }   r6   zBaseContext.Eventc                 C   s    ddl m} |||||  dS )zReturns a barrier objectr   )Barrierr'   )r5   r@   r*   )r   partiesactiontimeoutr@   r   r   r   r@      s   zBaseContext.Barrierr   c                 C   r8   )Returns a queue objectr   )Queuer'   )queuesrE   r*   )r   maxsizerE   r   r   r   rE      r;   zBaseContext.Queuec                 C   r8   )rD   r   )JoinableQueuer'   )rF   rH   r*   )r   rG   rH   r   r   r   rH      r;   zBaseContext.JoinableQueuec                 C   r3   )rD   r   )SimpleQueuer'   )rF   rI   r*   )r   rI   r   r   r   rI      r6   zBaseContext.SimpleQueuer   c                 C   s<   ddl m} ||||||||||	|
||||||||  dS )zReturns a process pool objectr   )Pool)context)poolrJ   r*   )r   	processesinitializerinitargsmaxtasksperchildrC   soft_timeoutlost_worker_timeoutmax_restartsmax_restart_freqon_process_upon_process_downon_timeout_seton_timeout_cancelthreads	semaphoreputlocksallow_restartrJ   r   r   r   rJ      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]      r;   zBaseContext.RawValuec                 C   s   ddl m} |||S )zReturns a shared arrayr   )RawArray)r^   ra   )r   r_   size_or_initializerra   r   r   r   ra      s   
zBaseContext.RawArrayc                 O   s4   ddl m} |dd}||g|R ||  dS )z$Returns a synchronized shared objectr   )Valuer:   Tr:   r(   )r^   rc   getr*   )r   r_   r`   kwargsrc   r:   r   r   r   rc      s
   zBaseContext.Valuec                 O   s,   ddl m} |dd}|||||  dS )z#Returns a synchronized shared arrayr   )Arrayr:   Trd   )r^   rg   re   r*   )r   r_   rb   r`   rf   rg   r:   r   r   r   rg      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.
        r   frozenFr   )freeze_supportN)r   r   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)utilrl   )r   rl   r   r   r   rl      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)rm   rn   )r   levelrn   r   r   r   rn         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)rk   rt   )r   
executablert   r   r   r   rt      s   zBaseContext.set_executablec                 C   rs   )zkSet list of module names to try to load in forkserver process.
        This is really just a hint.
        r   )set_forkserver_preloadN)
forkserverrv   )r   module_namesrv   r   r   r   rv      s   z"BaseContext.set_forkserver_preloadc                 C   s@   |d u r| S zt | }W n ty   td| w |  |S )Nzcannot find context for %r)_concrete_contextsr   r   _check_available)r   methodr(   r   r   r   r*      s   zBaseContext.get_contextc                 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{   r   r   r   set_start_method   s   zBaseContext.set_start_methodc                 C   s   |   pddkS )Nfork)r   r   r   r   r   forking_is_enabled      zBaseContext.forking_is_enabledc                 C   s<   |sddl m} |r| jddd d S ttt d S d S )Nr   )supports_execrk   T)force)_extr   r   warningswarnRuntimeWarning
W_NO_EXECV)r   r=   r   r   r   r   forking_enable   s   zBaseContext.forking_enablec                 C   s   d S r|   r   r   r   r   r   rz     s   zBaseContext._check_available)TFFr|   )r   )NN)r   )NNr   NNNNNr   NNNNTNFFF).__name__
__module____qualname__r   r   r   r   r   r	   r
   staticmethodr   current_processactive_childrenhasattrr   r   r-   r.   r4   r7   r9   r<   r>   r?   r@   rE   rH   rI   rJ   r]   ra   rc   rg   ri   rl   rn   rr   rt   rv   r*   r   r   r   r   rz   r   r   r   r   r   $   s`    
















	r   c                   @   s   e Zd ZdZedd ZdS )ProcessNc                 C   s   t  j| S r|   )_default_contextr*   r   _Popen)process_objr   r   r   r     r   zProcess._Popenr   r   r   _start_methodr   r   r   r   r   r   r         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 r|   )r   _actual_context)r   rK   r   r   r   __init__  s   
zDefaultContext.__init__Nc                    s0   |d u r| j d u r| j| _ | j S tt| |S r|   )r   r   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)r   RuntimeErrorr*   )r   r{   r   r   r   r   r   $  s   zDefaultContext.set_start_methodc                 C   s"   | j d u r|r	d S | j| _ | j jS r|   )r   r   r}   r~   r   r   r   r   ,  s
   
zDefaultContext.get_start_methodc                 C   s2   t jdkrdgS ddlm} |jrg dS ddgS )Nr   rk   r   	reductionr   rk   rw   r   )r   r   rq   r   HAVE_SEND_HANDLEr   r   r   r   r   get_all_start_methods3  s   
z$DefaultContext.get_all_start_methodsr|   r   )
r   r   r   r   r   r*   r   r   r   __classcell__r   r   r   r   r     s    

r   c                 c   s     | ]}|d  dkr|V  qdS )r   _Nr   ).0xr   r   r   	<genexpr>=  s    r   r   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   H  rp   zForkProcess._PopenNr   r   r   r   r   r   E  r   r   c                   @   r   )SpawnProcessrk   c                 C   r   r   )popen_spawn_posixr   r   r   r   r   r   P  rp   SpawnProcess._PopenNr   r   r   r   r   r   M  r   r   c                   @   r   )ForkServerProcessrw   c                 C   r   r   )popen_forkserverr   r   r   r   r   r   X  rp   zForkServerProcess._PopenNr   r   r   r   r   r   U  r   r   c                   @      e Zd ZdZeZdS )ForkContextr   N)r   r   r   r}   r   r   r   r   r   r   r   ]      r   c                   @   r   SpawnContextrk   Nr   r   r   r}   r   r   r   r   r   r   r   a  r   r   c                   @   s   e Zd ZdZeZdd ZdS )ForkServerContextrw   c                 C   s   ddl m} |jstdd S )Nr   r   z%forkserver start method not available)rq   r   r   r   r   r   r   r   rz   i  s   z"ForkServerContext._check_availableN)r   r   r   r}   r   r   rz   r   r   r   r   r   e  s    r   r   r   c                   @   r   )r   rk   c                 C   r   r   )popen_spawn_win32r   r   r   r   r   r   z  rp   r   Nr   r   r   r   r   r   w  r   c                   @   r   r   r   r   r   r   r   r     r   rk   c                 C   s   t |  t_d S r|   )ry   r   r   )r{   r   r   r   _force_start_method  s   r   c                   C   s   t tdd S )Nspawning_popen)rj   _tlsr   r   r   r   get_spawning_popen  s   r   c                 C   s
   | t _d S r|   )r   r   )r   r   r   r   set_spawning_popen  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_spawning  s   
r   )%r   r   	threadingr   rq   r   __all__r   
exceptionsr   r   r   r   r   r	   r
   r   BaseProcessr   r   listdirr   r   r   r   r   r   r   ry   r   r   localr   r   r   r   r   r   r   r   <module>   sF    $
 j(

