U
    .eb3                     @   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r2G 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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   )processzIforce_execv is not supported as the billiard C extension is not installed)ProcessErrorBufferTooShortTimeoutErrorAuthenticationErrorTimeLimitExceededSoftTimeLimitExceededWorkerLostErrorc                   @   sX  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rLdd Zndd Zdd ZdEd	d
Zdd Zdd ZdFddZdGddZdHddZdd ZdIddZdJddZdKddZd d! ZdLd#d$Zd%d& Zd'd( Zd)d* Z d+d, Z!d-d. Z"d/d0 Z#dMd1d2Z$d3d4 Z%d5d6 Z&d7d8 Z'dNd9d:Z(dOd;d<Z)dPd=d>Z*d?d@ Z+dAdB Z,dCdD Z-dS )QBaseContext	cpu_countc                 C   s"   t  }|dkrtdn|S dS )z(Returns the number of CPUs in the systemNcannot determine number of cpus)osr   NotImplementedError)selfnum r   4/tmp/pip-unpacked-wheel-39f38r4u/billiard/context.pyr   2   s    
zBaseContext.cpu_countc              
   C   s   t jdkr<zttjd }W q ttfk
r8   d}Y qX ndt jksPt jdkrd}t jdkrfd| }z&t|}t| }W 5 Q R X W q tk
r   d}Y qX n.zt	d}W n tt
tfk
r   d}Y nX |d	kr|S td
d S )Nwin32ZNUMBER_OF_PROCESSORSr   Z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   Zcommpr   r   r   r   :   s*    


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)Z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   ZduplexZ	rnonblockZ	wnonblockr*   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,   d   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.   i   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/   n   s    zBaseContext.Conditionr   c                 C   s   ddl m} |||  dS )zReturns a semaphore objectr   )	Semaphorer$   )r-   r1   r&   )r   valuer1   r   r   r   r1   s   s    zBaseContext.Semaphorec                 C   s   ddl m} |||  dS )z"Returns a bounded semaphore objectr   )BoundedSemaphorer$   )r-   r3   r&   )r   r2   r3   r   r   r   r3   x   s    zBaseContext.BoundedSemaphorec                 C   s   ddl m} ||  dS )zReturns an event objectr   )Eventr$   )r-   r4   r&   )r   r4   r   r   r   r4   }   s    zBaseContext.Eventc                 C   s    ddl m} |||||  dS )zReturns a barrier objectr   )Barrierr$   )r-   r5   r&   )r   partiesactiontimeoutr5   r   r   r   r5      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:      s    zBaseContext.Queuec                 C   s   ddl m} |||  dS )r9   r   )JoinableQueuer$   )r;   r=   r&   )r   r<   r=   r   r   r   r=      s    zBaseContext.JoinableQueuec                 C   s   ddl m} ||  dS )r9   r   )SimpleQueuer$   )r;   r>   r&   )r   r>   r   r   r   r>      s    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maxtasksperchildr8   Zsoft_timeoutZlost_worker_timeoutZmax_restartsZmax_restart_freqZon_process_upZon_process_downZon_timeout_setZon_timeout_cancelthreadsZ	semaphoreZputlocksZallow_restartr?   r   r   r   r?      s"    
         zBaseContext.Poolc                 G   s   ddl m} ||f| S )zReturns a shared objectr   )RawValue)sharedctypesrC   )r   typecode_or_typeargsrC   r   r   r   rC      s    zBaseContext.RawValuec                 C   s   ddl m} |||S )zReturns a shared arrayr   )RawArray)rD   rG   )r   rE   size_or_initializerrG   r   r   r   rG      s    zBaseContext.RawArrayc                 O   s2   ddl m} |dd}||f|||  dS )z$Returns a synchronized shared objectr   )Valuer0   Tr0   r%   )rD   rI   getr&   )r   rE   rF   kwargsrI   r0   r   r   r   rI      s
    zBaseContext.Valuec                 O   s,   ddl m} |dd}|||||  dS )z#Returns a synchronized shared arrayr   )Arrayr0   TrJ   )rD   rM   rK   r&   )r   rE   rH   rF   rL   rM   r0   r   r   r   rM      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.
        r   frozenFr   )freeze_supportN)r   r   getattrspawnrO   )r   rO   r   r   r   rO      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)utilrR   )r   rR   r   r   r   rR      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)rS   rT   )r   levelrT   r   r   r   rT      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)rQ   rX   )r   
executablerX   r   r   r   rX      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)
forkserverrZ   )r   Zmodule_namesrZ   r   r   r   rZ      s    z"BaseContext.set_forkserver_preloadc                 C   sF   |d kr| S zt | }W n  tk
r8   td| Y nX |  |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d S )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)rc   r   r   r   r   forking_is_enabled   s    zBaseContext.forking_is_enabledc                 C   s6   |s2ddl m} |r$| jddd nttt d S )Nr   )supports_execrQ   T)force)Z_extri   re   warningswarnRuntimeWarning
W_NO_EXECV)r   r2   ri   r   r   r   forking_enable   s
    zBaseContext.forking_enablec                 C   s   d S r_   r   rg   r   r   r   r]     s    zBaseContext._check_available)TFF)N)r   )r   )NN)r   )r   )NNr   NNNNNr   NNNNTNFF)N)N)F)N).__name__
__module____qualname__r   r   r   r   r   r	   r
   staticmethodr   current_processactive_childrenhasattrr   r   r)   r*   r,   r.   r/   r1   r3   r4   r5   r:   r=   r>   r?   rC   rG   rI   rM   rO   rR   rT   rW   rX   rZ   r&   rc   re   rh   ro   r]   r   r   r   r   r   $   sp   










                        





	r   c                   @   s   e Zd ZdZedd ZdS )ProcessNc                 C   s   t  j| S r_   )_default_contextr&   rw   _Popen)process_objr   r   r   ry     s    zProcess._Popenrp   rq   rr   Z_start_methodrs   ry   r   r   r   r   rw     s   rw   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_   )rx   _actual_context)r   r@   r   r   r   __init__  s    zDefaultContext.__init__Nc                    s4   |d kr | j d kr| j| _ | j S tt| |S d S r_   )r}   rx   superr|   r&   rd   	__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)r}   RuntimeErrorr&   )r   r^   rj   r   r   r   re   $  s    zDefaultContext.set_start_methodc                 C   s"   | j d kr|rd S | j| _ | j jS r_   )r}   rx   r`   ra   r   r   r   rc   ,  s
    
zDefaultContext.get_start_methodc                 C   s8   t jdkrdgS ddlm} |jr,dddgS ddgS d S )Nr   rQ   r   	reductionrf   r[   )r   r   rV   r   HAVE_SEND_HANDLEr   r   r   r   r   get_all_start_methods3  s    

z$DefaultContext.get_all_start_methods)N)F)F)
rp   rq   rr   rw   r~   r&   re   rc   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                   @   s   e Zd ZdZedd ZdS )ForkProcessrf   c                 C   s   ddl m} || S Nr   )Popen)Z
popen_forkr   rz   r   r   r   r   ry   H  s    zForkProcess._PopenNr{   r   r   r   r   r   E  s   r   c                   @   s   e Zd ZdZedd ZdS )SpawnProcessrQ   c                 C   s   ddl m} || S r   )Zpopen_spawn_posixr   r   r   r   r   ry   P  s    SpawnProcess._PopenNr{   r   r   r   r   r   M  s   r   c                   @   s   e Zd ZdZedd ZdS )ForkServerProcessr[   c                 C   s   ddl m} || S r   )Zpopen_forkserverr   r   r   r   r   ry   X  s    zForkServerProcess._PopenNr{   r   r   r   r   r   U  s   r   c                   @   s   e Zd ZdZeZdS )ForkContextrf   N)rp   rq   rr   r`   r   rw   r   r   r   r   r   ]  s   r   c                   @   s   e Zd ZdZeZdS SpawnContextrQ   Nrp   rq   rr   r`   r   rw   r   r   r   r   r   a  s   r   c                   @   s   e Zd ZdZeZdd ZdS )ForkServerContextr[   c                 C   s   ddl m} |jstdd S )Nr   r   z%forkserver start method not available)rV   r   r   r   r   r   r   r   r]   i  s    z"ForkServerContext._check_availableN)rp   rq   rr   r`   r   rw   r]   r   r   r   r   r   e  s   r   )rf   rQ   r[   rf   c                   @   s   e Zd ZdZedd ZdS )r   rQ   c                 C   s   ddl m} || S r   )Zpopen_spawn_win32r   r   r   r   r   ry   z  s    r   Nr{   r   r   r   r   r   w  s   c                   @   s   e Zd ZdZeZdS r   r   r   r   r   r   r     s   rQ   c                 C   s   t |  t_d S r_   )r\   rx   r}   )r^   r   r   r   _force_start_method  s    r   c                   C   s   t tdd S )Nspawning_popen)rP   _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 krtdt| j d S )NzF%s objects should only be shared between processes through inheritance)r   r   typerp   )objr   r   r   assert_spawning  s    
r   )%r   r   	threadingrk   rV   r   __all__rn   
exceptionsr   r   r   r   r   r	   r
   r   ZBaseProcessrw   r|   listdirr   r   r   r   r   r   r   r\   rx   r   localr   r   r   r   r   r   r   r   <module>   sF   
$ j(
 