3
]ð]ó  ã               @   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j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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Ú_thread_will_exitÚ	threadingÚLockÚ_lock)ÚselfÚintervalZmin_intervalÚtargetÚname© r   ú</tmp/pip-build-20mum3z4/pymongo/pymongo/periodic_executor.pyÚ__init__   s    zPeriodicExecutor.__init__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 |s®t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   Úopen5   s&    zPeriodicExecutor.openc             C   s
   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   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   ]   s
    
zPeriodicExecutor.joinc             C   s
   d| _ dS )z!Execute the target function soon.TN)r   )r   r   r   r   Úwakee   s    zPeriodicExecutor.wakec             C   s
   || _ d S )N)r   )r   Znew_intervalr   r   r   Úupdate_intervali   s    z PeriodicExecutor.update_intervalc          	   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_stopl   s
    zPeriodicExecutor.__should_stopc             C   s’   xŒ| j ƒ sŒy| jƒ sd| _P W n,   | j� d| _d| _W d Q R X ‚ Y nX tƒ | j }x*| j r‚tƒ |k r‚tj| j	ƒ | j
rZP qZW d| _
qW d S )NTF)Ú_PeriodicExecutor__should_stopr   r   r   r   Ú_timer   r   Úsleepr   r   )r   Údeadliner   r   r   r   s   s     
zPeriodicExecutor._run)N)N)N)Ú__name__Ú
__module__Ú__qualname__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)Úexecutorr0   r   r   r   r   “   s    r   c             C   s   t j| ƒ d S )N)r2   Úremove)r0   r   r   r   r1   ˜   s    r1   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é   )r2   Úlistr$   r   )Z	executorsr0   r4   r   r   r   Ú_shutdown_executorsœ   s    

r8   )Ú__doc__Úatexitr   r   r   Zpymongo.monotonicr*   Úobjectr   Úsetr2   r   r1   r8   Úregisterr   r   r   r   Ú<module>   s   w