o Sb@s^dZddlZddlZddlZddlmZmZGdddeZe Z ddZ dd Z d d Z dS) z-Run a target function on a background thread.N)AnyOptionalc@seZdZdddZddZdddZdd eddfd d Zdd ee ddfd dZ dddZ de ddfddZ dddZ ddZddZdS)PeriodicExecutorNcCsDd|_||_||_||_d|_d|_||_d|_d|_t |_ dS)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 threadingLock_lock)selfintervalZ min_intervaltargetnamer@/tmp/pip-target-onvjaxws/lib/python/pymongo/periodic_executor.py__init__szPeriodicExecutor.__init__cCsd|jj|jt|fS)Nz<%s(name=%s) object at 0x%x>) __class____name__r idrrrr__repr__4szPeriodicExecutor.__repr__returnc Cs|j)|jrz|jdusJ|jWn tyYnwd|_d|_Wdn1s/wYd}z |jo>|j}Wn tyIYnw|shtj|j |j d}d|_ t ||_t||dSdS)zgStart. Multiple calls have no effect. Not safe to call from multiple threads at once. NF)rrT)rr r joinReferenceErrorr is_aliverThread_runr daemonweakrefproxy_register_executorstart)rstartedthreadrrropen7s0    zPeriodicExecutor.opendummycC d|_dS)zStop. To restart, call open(). The dummy parameter allows an executor's close method to be a weakref callback; see monitor.py. TN)r )rr+rrrcloseXs zPeriodicExecutor.closetimeoutc Cs:|jdurz |j|WdSttfyYdSwdSN)r rr RuntimeError)rr.rrrr`s zPeriodicExecutor.joincCr,)z!Execute the target function soon.TN)rrrrrwakehs zPeriodicExecutor.wake new_intervalcCs ||_dSr/)r)rr2rrrupdate_intervall z PeriodicExecutor.update_intervalcCs d|_dS)NT)r rrrr skip_sleepor4zPeriodicExecutor.skip_sleepcCsL|j|jrd|_ WddS WddS1swYdSNTF)rr r rrrrZ __should_stoprs$zPeriodicExecutor.__should_stopc Cs|sez |sd|_WdSWn!ty1|jd|_d|_Wd1s+wYw|jr9d|_n#t|j }|js\t|kr\t |j |j rSn |js\t|ksId|_ |rdSdSr6) _PeriodicExecutor__should_stoprr BaseExceptionrr r time monotonicrsleeprr)rdeadlinerrrr"ys2   zPeriodicExecutor._runr/)rN)r __module__ __qualname__rrr*rr-rintrr1r3r5r7r"rrrrrs  !   rcCst|t}t|dSr/)r$ref_on_executor_deleted _EXECUTORSadd)executorr@rrrr&s r&cCst|dSr/)rBremove)r@rrrrAsrAcCsVtdurdStt}|D] }|}|r|q |D] }|}|r&|dqd}dS)N)rBlistr-r)Z executorsr@rDrrr_shutdown_executorss rH)__doc__rr9r$typingrrobjectrsetrBr&rArHrrrrs