o
    svXj’3  ã                   @   s¢  d dl mZ 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eƒ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rŸG 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 ))é    )Úabsolute_importNé   )Ú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   úM/var/www/html/myproject/venv/lib/python3.10/site-packages/billiard/context.pyr   4   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 )z½Returns 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   ÚManagerV   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/   a   s   zBaseContext.Pipec                 C   ó   ddl m} ||  ¡ d�S )z#Returns a non-recursive lock objectr   )ÚLockr(   )Úsynchronizer5   r+   )r   r5   r   r   r   r5   f   ó   zBaseContext.Lockc                 C   r4   )zReturns a recursive lock objectr   )ÚRLockr(   )r6   r8   r+   )r   r8   r   r   r   r8   k   r7   zBaseContext.RLockNc                 C   ó   ddl m} |||  ¡ d�S )zReturns a condition objectr   )Ú	Conditionr(   )r6   r:   r+   )r   Úlockr:   r   r   r   r:   p   ó   zBaseContext.Conditionr   c                 C   r9   )zReturns a semaphore objectr   )Ú	Semaphorer(   )r6   r=   r+   )r   Úvaluer=   r   r   r   r=   u   r<   zBaseContext.Semaphorec                 C   r9   )z"Returns a bounded semaphore objectr   )ÚBoundedSemaphorer(   )r6   r?   r+   )r   r>   r?   r   r   r   r?   z   r<   zBaseContext.BoundedSemaphorec                 C   r4   )zReturns an event objectr   )ÚEventr(   )r6   r@   r+   )r   r@   r   r   r   r@      r7   zBaseContext.Eventc                 C   s    ddl m} |||||  ¡ d�S )zReturns a barrier objectr   )ÚBarrierr(   )r6   rA   r+   )r   ÚpartiesÚactionÚtimeoutrA   r   r   r   rA   „   s   zBaseContext.Barrierr   c                 C   r9   )úReturns a queue objectr   )ÚQueuer(   )ÚqueuesrF   r+   )r   ÚmaxsizerF   r   r   r   rF   ‰   r<   zBaseContext.Queuec                 C   r9   )rE   r   )ÚJoinableQueuer(   )rG   rI   r+   )r   rH   rI   r   r   r   rI   Ž   r<   zBaseContext.JoinableQueuec                 C   r4   )rE   r   )ÚSimpleQueuer(   )rG   rJ   r+   )r   rJ   r   r   r   rJ   “   r7   zBaseContext.SimpleQueuer   c                 C   s<   ddl m} ||||||||||	|
||||||||  ¡ d�S )zReturns a process pool objectr   )ÚPool)Úcontext)ÚpoolrK   r+   )r   Ú	processesÚinitializerÚinitargsÚmaxtasksperchildrD   Ú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_restartrK   r   r   r   rK   ˜   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_   rb   )r   r`   Úsize_or_initializerrb   r   r   r   rb   ¬   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_   rd   Úgetr+   )r   r`   ra   Úkwargsrd   r;   r   r   r   rd   ±   s
   ÿzBaseContext.Valuec                 O   s,   ddl m} | dd¡}|||||  ¡ d�S )z#Returns a synchronized shared arrayr   )ÚArrayr;   Tre   )r_   rh   rf   r+   )r   r`   rc   ra   rg   rh   r;   r   r   r   rh   ¸   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 )z‹Check 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Úspawnrj   )r   rj   r   r   r   rj   ¿   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)Úutilrm   )r   rm   r   r   r   rm   Ç   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)rn   ro   )r   Úlevelro   r   r   r   ro   Î   ó   zBaseContext.log_to_stderrc                 C   s   ddl m} dS )zVInstall support for sending connections and sockets
        between processes
        r   )r0   N)Ú r0   )r   r0   r   r   r   Úallow_connection_picklingÓ   s   z%BaseContext.allow_connection_picklingc                 C   ó   ddl m} ||ƒ dS )zÌSets 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)rl   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   Úmodule_namesrw   r   r   r   rw   ã   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_execrl   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   r{     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/   r5   r8   r:   r=   r?   r@   rA   rF   rI   rJ   rK   r^   rb   rd   rh   rj   rm   ro   rs   ru   rw   r+   r�   rƒ   r†   r�   r{   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   rL   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   rl   r   ©Ú	reduction©r„   rl   rx   r„   )r   r   rr   r§   ÚHAVE_SEND_HANDLE©r   r§   r   r   r   Úget_all_start_methods5  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š   J  rq   zForkProcess._PopenNrœ   r   r   r   r   r²   G  rž   r²   c                   @   r±   )ÚSpawnProcessrl   c                 C   r³   r´   )Úpopen_spawn_posixrµ   r·   r   r   r   rš   R  rq   úSpawnProcess._PopenNrœ   r   r   r   r   r¸   O  rž   r¸   c                   @   r±   )ÚForkServerProcessrx   c                 C   r³   r´   )Úpopen_forkserverrµ   r·   r   r   r   rš   Z  rq   zForkServerProcess._PopenNrœ   r   r   r   r   r»   W  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½   ©ÚSpawnContextrl   N©r‘   r’   r“   r~   r¸   r˜   r   r   r   r   rÁ   c  r¿   rÁ   c                   @   s   e Zd ZdZeZdd„ ZdS )ÚForkServerContextrx   c                 C   s   ddl m} |jstdƒ‚d S )Nr   r¦   z%forkserver start method not available)rr   r§   r©   r   rª   r   r   r   r{   k  s   ÿz"ForkServerContext._check_availableN)r‘   r’   r“   r~   r»   r˜   r{   r   r   r   r   rÃ   g  s    rÃ   r¨   r„   c                   @   r±   )r¸   rl   c                 C   r³   r´   )Úpopen_spawn_win32rµ   r·   r   r   r   rš   |  rq   rº   Nrœ   r   r   r   r   r¸   y  rž   c                   @   r½   rÀ   rÂ   r   r   r   r   rÁ   �  r¿   rl   c                 C   s   t |  t_d S r}   )rz   r™   r    )r|   r   r   r   Ú_force_start_method�  s   rÅ   c                   C   s   t tdd ƒS )NÚspawning_popen)rk   Ú_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Ì   )(Ú
__future__r   r   r   Ú	threadingr‹   rr   r   Ú__all__rŽ   Ú
exceptionsr   r   r   r   r	   r
   r   Úobjectr   ÚBaseProcessr˜   rŸ   ÚlistÚdirr   r²   r¸   r»   r¾   rÁ   rÃ   rz   r™   rÅ   ÚlocalrÇ   rÈ   rÉ   rÌ   r   r   r   r   Ú<module>   sH    $
 j(

ýÿ