pyqtSignal OR Queue with threading Timer for heartbeat?
pyqtSignal OR Queue with threading Timer for heartbeat?
I'm experimenting with creating a HeartBeat(threading._Timer)
class that is to notify the main class if a program is still alive or not.
HeartBeat(threading._Timer)
I was originally trying to leverage the pyqtSignal from PyQt4.QtCore because of the easiness and possible future use with updating a GUI. But I'm having a hard time getting the pyqtSignal to actually work. Below is what I have done thus far with the pyqtSignal, but I can't get the signals to work.
I was then thinking that I should use this opportunity to strengthen my multiprocessing.Queue
skills. I'm hoping you folks could help me get a good grasp on how to efficiently use the best method for this kind of task.
multiprocessing.Queue
I have four classes for easy reuse:
from PyQt4.QtCore import pyqtSignal,QObject
class HeartBeatSignals(QObject):
HeLives = pyqtSignal()
HesDeadJim = pyqtSignal()
import threading
import time
import multiprocessing as mp
class HeartBeat(threading._Timer):
def__init__(self,interval,function):
super(HeartBeat,self).__init__(interval,function)
self.signal = HeartBeatSignals()
def run(self):
while not self.finished.is_set():
result = self.function.is_alive()
self.finished.wait(self.interval)
if result:
print "He's Alive!"
self.signal.HeLives.emit()
else:
print "He's dead Jim.."
self.signal.hesDeadJim.emit()
class Worker(object):
def __init__(self):
'''beep-boop'''
def run(self):
while True:
print "working"
time.sleep(1)
class WATCHDOG(object):
def __init__(self):
worker = Worker()
self.w = mp.Process(target=worker.run)
self.w.start()
self.WorkerHeartBeat = HeartBeat(4.0,self.w)
self.WorkerHeartBeat.start()
self.WorkerHeartBeat.signal.HeLives.connect(self.dontcare)
self.WorkerHeartBeat.signal.HesDeadJim.connect(self.death2Jim)
def dontcare(self):
print "..really don't care."
def death2Jim(self):
print "KAAAAHHNNN!!!!"
W = WATCHDOG()
My original intent was to have the main thread be notified by the pyqtSignal and then restart that process and maybe put in an indicator light on the GUI that Process was killed, being restarted, is good to go kind of thing. BUT I suppose that pyqtSignal does not like being part of a class.
I suppose I could do the same thing with multiprocessing.Queue
or maybe multiprocessing.Condition
??
multiprocessing.Queue
multiprocessing.Condition
With the queue, would I have to setup a queue passed to each of the worker heartbeats I establish and then create a while loop in the main to q.get()
for each of them?
q.get()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.