o
    'hk&                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZ dZG dd	 d	ZG d
d dZe ZdddZdddZdddZdS )z-Run a target function on a background thread.    )annotationsN)AnyOptional)create_task)_create_lockFc                   @  sn   e Zd Z	d#d$d	d
Zd%ddZd&ddZd#d'ddZd#d(ddZd&ddZd)ddZ	d&dd Z
d&d!d"ZdS )*AsyncPeriodicExecutorNintervalfloatmin_intervaltargetr   nameOptional[str]c                 C  s4   d| _ || _|| _|| _d| _d| _|| _d| _dS )a  Run a target function periodically on a background task.

        If the target's return value is false, the executor stops.

        :param interval: Seconds between calls to `target`.
        :param min_interval: Minimum seconds between calls if `wake` is
            called very often.
        :param target: A function.
        :param name: A name to give the underlying task.
        FN)_event	_interval_min_interval_target_stopped_task_name_skip_sleepselfr   r
   r   r    r   U/var/www/html/olx_land/venv/lib/python3.10/site-packages/pymongo/periodic_executor.py__init__!   s   
zAsyncPeriodicExecutor.__init__returnstrc                 C  $   d| j j d| j dt| ddS N<z(name=z) object at 0xx>	__class____name__r   idr   r   r   r   __repr__;      $zAsyncPeriodicExecutor.__repr__Nonec                 C  sR   d| _ | jdu s| j r#| j s%| j s't|  | jd| _dS dS dS dS )z%Start. Multiple calls have no effect.FN)r   )r   r   done	cancelled
cancellingr   _runr   r&   r   r   r   open>   s   
zAsyncPeriodicExecutor.opendummyc                 C  s"   d| _ | jdur| j  dS dS zStop. To restart, call open().

        The dummy parameter allows an executor's close method to be a weakref
        callback; see monitor.py.
        TN)r   r   cancelr   r/   r   r   r   closeG   s   
zAsyncPeriodicExecutor.closetimeoutOptional[int]c                   s,   | j d urtj| j g|dI d H  d S d S )N)r4   )r   asynciowaitr   r4   r   r   r   joinQ   s   
zAsyncPeriodicExecutor.joinc                 C  
   d| _ dS z!Execute the target function soon.TNr   r&   r   r   r   wakeU      
zAsyncPeriodicExecutor.wakenew_intervalintc                 C  
   || _ d S Nr   r   r?   r   r   r   update_intervalY      
z%AsyncPeriodicExecutor.update_intervalc                 C  
   d| _ d S NTr   r&   r   r   r   
skip_sleep\   rF   z AsyncPeriodicExecutor.skip_sleepc                   s   | j s_| jr| j rtjz|  I d H sd| _ W d S W n ty)   d| _  w | jr1d| _n&t	 | j
 }| j sWt	 |k rWt| jI d H  | jrNn	| j sWt	 |k sAd| _| j rd S d S NTF)r   r   r,   r6   CancelledErrorr   BaseExceptionr   time	monotonicr   sleepr   r   r   deadliner   r   r   r-   _   s.   zAsyncPeriodicExecutor._runrB   r   r	   r
   r	   r   r   r   r   r   r   r   r)   r/   r   r   r)   r4   r5   r   r)   r?   r@   r   r)   )r$   
__module____qualname__r   r'   r.   r3   r9   r=   rE   rJ   r-   r   r   r   r   r       s    

	



r   c                   @  sx   e Zd Z	d&d'd	d
Zd(ddZd)ddZd&d*ddZd&d+ddZd)ddZd,ddZ	d)dd Z
d-d"d#Zd)d$d%ZdS ).PeriodicExecutorNr   r	   r
   r   r   r   r   c                 C  sB   d| _ || _|| _|| _d| _d| _|| _d| _d| _t	 | _
dS )a  Run a target function periodically on a background thread.

        If the target's return value is false, the executor stops.

        :param interval: Seconds between calls to `target`.
        :param min_interval: Minimum seconds between calls if `wake` is
            called very often.
        :param target: A function.
        :param name: A name to give the underlying thread.
        FN)r   r   r   r   r   _threadr   r   _thread_will_exitr   _lockr   r   r   r   r   y   s   zPeriodicExecutor.__init__r   r   c                 C  r   r   r"   r&   r   r   r   r'      r(   zPeriodicExecutor.__repr__r)   c              
   C  s  | j ) | jrz| jdusJ | j  W n	 ty   Y nw d| _d| _W d   n1 s/w   Y  d}z
| jo>| j }W n	 tyI   Y nw |stj| j	| j
d}d|_t|| _t|  z|  W dS  ty } zdt|v s{t rd| _W Y d}~dS  d}~ww dS )zgStart. Multiple calls have no effect.

        Not safe to call from multiple threads at once.
        NF)r   r   Tzinterpreter shutdown)r^   r]   r\   r9   ReferenceErrorr   is_alive	threadingThreadr-   r   daemonweakrefproxy_register_executorstartRuntimeErrorr   sysis_finalizing)r   startedthreader   r   r   r.      s@   zPeriodicExecutor.openr/   c                 C  r:   r0   )r   r2   r   r   r   r3      s   
zPeriodicExecutor.closer4   r5   c              	   C  s:   | j d urz	| j | W d S  ttfy   Y d S w d S rB   )r\   r9   r_   rh   r8   r   r   r   r9      s   
zPeriodicExecutor.joinc                 C  r:   r;   r<   r&   r   r   r   r=      r>   zPeriodicExecutor.waker?   r@   c                 C  rA   rB   rC   rD   r   r   r   rE      rF   z PeriodicExecutor.update_intervalc                 C  rG   rH   rI   r&   r   r   r   rJ      rF   zPeriodicExecutor.skip_sleepboolc                 C  sL   | j  | jrd| _	 W d    dS 	 W d    dS 1 sw   Y  d S rK   )r^   r   r]   r&   r   r   r   _should_stop   s   $zPeriodicExecutor._should_stopc                 C  s   |   sez|  sd| _W d S W n! ty1   | j d| _d| _W d     1 s+w   Y   w | jr9d| _n#t | j	 }| js\t |k r\t
| j | jrSn	| js\t |k sId| _|   rd S d S rK   )ro   r   r   rM   r^   r]   r   rN   rO   r   rP   r   r   rQ   r   r   r   r-      s2   
zPeriodicExecutor._runrB   rS   rT   rU   rV   rW   rX   )r   rn   )r$   rY   rZ   r   r'   r.   r3   r9   r=   rE   rJ   ro   r-   r   r   r   r   r[   x   s    
 
)



r[   executorr   r)   c                 C  s   t | t}t| d S rB   )rd   ref_on_executor_deleted
_EXECUTORSadd)rp   rq   r   r   r   rf   	  s   rf   rq   'weakref.ReferenceType[PeriodicExecutor]c                 C  s   t |  d S rB   )rs   remove)rq   r   r   r   rr     s   rr   c                  C  sV   t d u rd S tt } | D ]}| }|r|  q| D ]}| }|r&|d qd }d S )N   )rs   listr3   r9   )	executorsrq   rp   r   r   r   _shutdown_executors  s   
rz   )rp   r[   r   r)   )rq   ru   r   r)   rU   )__doc__
__future__r   r6   ri   ra   rN   rd   typingr   r   pymongo._asyncio_taskr   pymongo.lockr   _IS_SYNCr   r[   setrs   rf   rr   rz   r   r   r   r   <module>   s$   X 

