o
    Åîï]ó  ã                   @   sl   d Z ddlZddlZddlZddlZddlmZ G dd„ deƒZe	ƒ Z
dd„ Zdd	„ Zd
d„ Ze e¡ dS )z-Run a target function on a background thread.é    N)Útimec                   @   sR   e Zd Zddd„Zdd„ Zddd„Zddd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚPeriodicExecutorNc                 C   s>   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.

        :Parameters:
          - `interval`: Seconds between calls to `target`.
          - `min_interval`: Minimum seconds between calls if `wake` is
            called very often.
          - `target`: A function.
          - `name`: A name to give the underlying thread.
        FN)Ú_eventÚ	_intervalÚ_min_intervalÚ_targetÚ_stoppedÚ_threadÚ_nameÚ_thread_will_exitÚ	threadingÚLockÚ_lock)ÚselfÚintervalÚmin_intervalÚtargetÚname© r   úK/var/www/html/env/lib/python3.10/site-packages/pymongo/periodic_executor.pyÚ__init__   s   zPeriodicExecutor.__init__c              	   C   sÆ   | j �" | jrz| j ¡  W n	 ty   Y nw d| _d| _W d  ƒ n1 s(w   Y  d}z
| jo7| j ¡ }W n	 tyB   Y nw |satj| j	| j
d�}d|_t |¡| _t| ƒ | ¡  dS dS )zgStart. Multiple calls have no effect.

        Not safe to call from multiple threads at once.
        FN)r   r   T)r   r   r	   ÚjoinÚReferenceErrorr   Úis_aliver   ÚThreadÚ_runr
   ÚdaemonÚweakrefÚproxyÚ_register_executorÚstart)r   ÚstartedÚthreadr   r   r   Úopen5   s.   þôþûzPeriodicExecutor.openc                 C   ó
   d| _ dS )z˜Stop. To restart, call open().

        The dummy parameter allows an executor's close method to be a weakref
        callback; see monitor.py.
        TN)r   )r   Údummyr   r   r   ÚcloseU   s   
zPeriodicExecutor.closec              	   C   s:   | j d urz	| j  |¡ W d S  ttfy   Y d S w d S ©N)r	   r   r   ÚRuntimeError)r   Útimeoutr   r   r   r   ]   s   
þýzPeriodicExecutor.joinc                 C   r$   )z!Execute the target function soon.TN)r   ©r   r   r   r   Úwakee   s   
zPeriodicExecutor.wakec                 C   s
   || _ d S r'   )r   )r   Únew_intervalr   r   r   Úupdate_intervali   s   
z PeriodicExecutor.update_intervalc                 C   sL   | j � | jrd| _	 W d   ƒ dS 	 W d   ƒ dS 1 sw   Y  d S ©NTF)r   r   r   r*   r   r   r   Ú__should_stopl   s   ý$üzPeriodicExecutor.__should_stopc                 C   s²   |   ¡ sWz|  ¡ sd| _W d S W n   | j� d| _d| _W d   ƒ ‚ 1 s(w   Y  ‚ tƒ | j }| jsNtƒ |k rNt | j	¡ | j
rFn| jsNtƒ |k s<d| _
|   ¡ rd S d S r.   )Ú_PeriodicExecutor__should_stopr   r   r   r   Ú_timer   r   Úsleepr   r   )r   Údeadliner   r   r   r   s   s,   þ
þüýízPeriodicExecutor._runr'   )Ú__name__Ú
__module__Ú__qualname__r   r#   r&   r   r+   r-   r0   r   r   r   r   r   r      s    

 
r   c                 C   s   t  | t¡}t |¡ d S r'   )r   ÚrefÚ_on_executor_deletedÚ
_EXECUTORSÚadd)Úexecutorr7   r   r   r   r   “   s   r   c                 C   s   t  | ¡ d S r'   )r9   Úremove)r7   r   r   r   r8   ˜   s   r8   c                  C   sV   t d u rd S tt ƒ} | D ]}|ƒ }|r| ¡  q| D ]}|ƒ }|r&| d¡ qd }d S )Né   )r9   Úlistr&   r   )Ú	executorsr7   r;   r   r   r   Ú_shutdown_executorsœ   s   €
€r@   )Ú__doc__Úatexitr   r   r   Úpymongo.monotonicr1   Úobjectr   Úsetr9   r   r8   r@   Úregisterr   r   r   r   Ú<module>   s   w