H
Harald Armin Massa
I need to do some synchronisations like in a cron.job
import time
from threading import Thread
class updater(Thread):
def run(self):
while True:
do_updates()
time.sleep(600)
updater().start()
# regular program flow continues ....
print "tralalala"
while True:
do_very_important_stuff()
if ask_user_if_I_should_stop():
break
print "programm stopped because YOU decided ..."
#################################
BUT ... the updater() thread keeps running. The software will not end
until forced with keyboard interrupt.
I would like to have a way to stop that thread in a automatic way.
one obvious solution would be:
import time
from threading import Thread
import Queue
class updater(Thread):
stopper=Queue.Queue()
def run(self):
self.timegone=0
while self.stopper.empty():
time.sleep(0.5)
self.timegone+=1
if self.timegone>=600:
print "you waited, time passed, I will do updates"
do_updates()
self.timegone=0
print "updater stopped also"
myupdater=updater()
myupdater.start()
while True:
print "doing important stuff"
do_very_important_stuff()
a=raw_input("stop?")
if a=="y":
myupdater.stopper.put("stop this thread, will you??")
break
print "programm stopped because YOU decided ..."
############
I use a Queue to communicate the stop-message to the updater-thread.
Queue is documented as being thread-safe, so no hobbling with mutex or
stuff.
BUT... just to look at that queue the thread has to be activated. And
.... the usual run-time of the software will be around 10hours, the
update has to be done 20 times. And only in the evening the programm
will be ended (shutdown of the computer). And more over: it will be in
the background. time.sleep() takes essentially no processor time.
Checking the queue will not stop the computer, but takes mor than
necessary.
Is there any better approach ?
Harald
import time
from threading import Thread
class updater(Thread):
def run(self):
while True:
do_updates()
time.sleep(600)
updater().start()
# regular program flow continues ....
print "tralalala"
while True:
do_very_important_stuff()
if ask_user_if_I_should_stop():
break
print "programm stopped because YOU decided ..."
#################################
BUT ... the updater() thread keeps running. The software will not end
until forced with keyboard interrupt.
I would like to have a way to stop that thread in a automatic way.
one obvious solution would be:
import time
from threading import Thread
import Queue
class updater(Thread):
stopper=Queue.Queue()
def run(self):
self.timegone=0
while self.stopper.empty():
time.sleep(0.5)
self.timegone+=1
if self.timegone>=600:
print "you waited, time passed, I will do updates"
do_updates()
self.timegone=0
print "updater stopped also"
myupdater=updater()
myupdater.start()
while True:
print "doing important stuff"
do_very_important_stuff()
a=raw_input("stop?")
if a=="y":
myupdater.stopper.put("stop this thread, will you??")
break
print "programm stopped because YOU decided ..."
############
I use a Queue to communicate the stop-message to the updater-thread.
Queue is documented as being thread-safe, so no hobbling with mutex or
stuff.
BUT... just to look at that queue the thread has to be activated. And
.... the usual run-time of the software will be around 10hours, the
update has to be done 20 times. And only in the evening the programm
will be ended (shutdown of the computer). And more over: it will be in
the background. time.sleep() takes essentially no processor time.
Checking the queue will not stop the computer, but takes mor than
necessary.
Is there any better approach ?
Harald