3
O6b                 @   sZ   d 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dS )z-Run a target function on a background thread.    N)timec               @   sb   e Zd ZdddZ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d ZdS )PeriodicExecutorNc             C   sD   d| _ || _|| _|| _d| _d| _|| _d| _d| _t	j
 | _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_skip_sleep_thread_will_exit	threadingLock_lock)selfintervalZmin_intervaltargetname r   X/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/periodic_executor.py__init__   s    zPeriodicExecutor.__init__c             C   s   d| j j| jt| f S )Nz<%s(name=%s) object at 0x%x>)	__class____name__r
   id)r   r   r   r   __repr__5   s    zPeriodicExecutor.__repr__c             C   s   | j < | jr2y| jj  W n tk
r0   Y nX d| _d| _W dQ R X d}y| jo\| jj }W n tk
rt   Y nX |stj| j	| j
d}d|_tj|| _t|  |j  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   open9   s&    zPeriodicExecutor.openc             C   s
   d| _ 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   dummyr   r   r   closeY   s    zPeriodicExecutor.closec             C   s8   | j d k	r4y| j j| W n ttfk
r2   Y nX d S )N)r	   r   r   RuntimeError)r   timeoutr   r   r   r   a   s
    
zPeriodicExecutor.joinc             C   s
   d| _ dS )z!Execute the target function soon.TN)r   )r   r   r   r   wakei   s    zPeriodicExecutor.wakec             C   s
   || _ d S )N)r   )r   Znew_intervalr   r   r   update_intervalm   s    z PeriodicExecutor.update_intervalc             C   s
   d| _ d S )NT)r   )r   r   r   r   
skip_sleepp   s    zPeriodicExecutor.skip_sleepc          	   C   s&   | j  | jrd| _dS dS Q R X d S )NTF)r   r   r   )r   r   r   r   Z__should_stops   s
    zPeriodicExecutor.__should_stopc             C   s   x| j  sy| j sd| _P W n,   | j d| _d| _W d Q R X  Y nX | jrZd| _n8t | j }x*| j rt |k rtj	| j
 | jrhP qhW d| _qW d S )NTF)_PeriodicExecutor__should_stopr   r   r   r   r   _timer   r   sleepr   r   )r   deadliner   r   r   r   z   s$    
zPeriodicExecutor._run)N)N)N)r   
__module____qualname__r   r   r'   r)   r   r,   r-   r.   r/   r   r   r   r   r   r      s   
 

r   c             C   s   t j| t}tj| d S )N)r!   ref_on_executor_deleted
_EXECUTORSadd)executorr5   r   r   r   r#      s    r#   c             C   s   t j|  d S )N)r7   remove)r5   r   r   r   r6      s    r6   c              C   s^   t d krd S tt } x| D ]}| }|r|j  qW x | D ]}| }|r:|jd q:W d }d S )N   )r7   listr)   r   )Z	executorsr5   r9   r   r   r   _shutdown_executors   s    

r=   )__doc__r   r   r!   Zpymongo.monotonicr0   objectr   setr7   r#   r6   r=   r   r   r   r   <module>   s    