【发布时间】:2017-09-22 05:03:29
【问题描述】:
我遇到了一些问题,到目前为止,我的许多测试/搜索都让我无处可去。
我有一个 Flask 应用程序,它为运行在树莓派上的小型警报系统提供 GUI。 GUI 允许将警报(目前在 GPIO 引脚上的运动传感器)作为后台进程启动。这运行良好,如果检测到它会发送电子邮件。 我正在尝试添加一种可以播放警笛声(或 100 分贝的黑色金属歌曲,尚未决定:p)的方法。 该方法本身在一个类中并且工作正常,我可以从 GUI 激活/停用它作为另一个后台进程。
但是,我无法让警报过程自动触发它。不知道为什么,我没有错误,只是没有触发:
class Alarm(Process):
def __init__(self):
super().__init__()
self.exit = Event()
def shutdown(self):
self.exit.set()
def run(self):
Process.__init__(self)
#code
#if motion detected sendmail, and run the siren:
siren = Siren() #actually, problem here: I need to import the class
#and declare this in the main Flask app file, so as
#to be able to control it from the GUI. Apparently I
#can't declare it in both python files (sounds
#obvious though)
siren.start()
#then keep looping
和Siren类类似:
class Siren(Process):
def __init__(self):
super().__init__()
def run(self):
Process.__init__(self)
#code using pyaudio
我猜我对类和方法的基本理解是这里的罪魁祸首。
我也尝试过使用事件,所以不是siren.start(),而是e = Event() 和e.set(),但我无法在后台运行Siren 类并让它拾取该事件e.wait()。
谁能指出我正确的方向?
谢谢!
【问题讨论】:
标签: python flask background-process