Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python create named timer

try:
    from threading import Timer
except ImportError:
    from threading import _Timer as Timer  # Python <3.3

def named_timer(name, interval, function, *args, **kwargs):
    """Factory function to create named Timer objects.

      Named timers call a function after a specified number of seconds:

          t = named_timer('Name', 30.0, function)
          t.start()
          t.cancel()  # stop the timer's action if it's still waiting
    """
    timer = Timer(interval, function, *args, **kwargs)
    timer.name = name
    return timer

if __name__ == '__main__':

    def func():
        print('func() called')

    timer = named_timer('Fidgit', 3, func)
    print('timer.name: {!r}'.format(timer.name))  # -> timer.name: 'Fidgit'
    timer.run()  # Causes "func() called" to be printed after a few seconds.
Comment

PREVIOUS NEXT
Code Example
Python :: maximum of a list in python recursively 
Python :: unique character 02 
Python :: merge sort dictionary python 
Python :: scikit learn lazy predict 
Python :: pass method 
Python :: how to get mid time of given time in python 
Python :: python set vs tuple performance 
Python :: python convert dataframe target to numbers 
Python :: how to plot a single centroid 
Python :: python certificate verify failed unable to get local issuer certificate nltk 
Python :: counter and element of list for loop python 
Python :: python subprocess call with no environment variable 
Python :: Broadcasting with NumPy Arrays Plotting a two-dimensional function Example 
Python :: Python NumPy copyto function example copy elements from a source array to a destination array. 
Python :: python text file contains 
Python :: Python NumPy asanyarray Function Example Tuple to an array 
Python :: Python NumPy hstack Function Example with 1d array 
Python :: configure socketio static file python specific content type 
Python :: python how to loop through array 
Python :: python interpreter after running a python file 
Python :: how to nest try/except statements 
Python :: NumPy packbits Syntax 
Python :: pandas aggregate rename column 
Python :: gensim prepare corpus 
Python :: bouton 
Python :: Python script to download all images from a website to a specified folder with BeautifulSoup 
Python :: create multiple marks python for python 
Python :: phlib examples python 
Python :: How to correctly call url_for and specify path parameters 
Python :: tdlib python 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =