Initiating a Timer-Interrupted Thread


wakeCall = threading.Timer(waitTime, \                 clean_queue, (qPath ,)) wakeCall.start()

Common threads invoked on Linux servers are the timer threads to clean up resources, provide notification, and check status, as well as many other functions. The threading module included with Python provides an easy way of creating a simple timer-interrupted thread.

The Timer(interval, func [,args [, kwargs]]) method of the threading module creates a new timer-interrupted thread object. The interval specifies the number of seconds to wait before executing the function specified in the func argument.

Once the new timer-interrupted thread object is created, it can be started at any time using the start method of the object. Once the start method is invoked, the thread will wait the specified timer interval and then begin execution.

Note

A timer thread can be cancelled after it is started, using the cancel() method of the object, provided that the function has not yet been executed.


import threading import os def clean_queue (qPath):     jobList = os.listdir(qPath)     for j in jobList:         delPath = "%s/%s" % (qPath, j)         os.remove(delPath)         print "Removing " + delPath qPath = "/print/queue01" waitTime = 600 #10 minutes #Create timer thread wakeCall = threading.Timer(waitTime, \                 clean_queue, (qPath ,)) #Start timer thread wakeCall.start()


timer_thread.py

Removing /print/queue01/102.txt Removing /print/queue01/103.txt Removing /print/queue01/104.txt Removing /print/queue01/105.txt Removing /print/queue01/106.txt Removing /print/queue01/107.txt


Output from timer_thread.py code



Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net