Python : 从后台进程启动后台进程
Python : starting background process from background process
我遇到了一些问题,我的许多 tests/searches 到目前为止都没有带我走。
我在 raspberry pi 上有一个 Flask 应用程序,它为小型报警系统 运行 提供了 GUI。
GUI 允许将警报(目前是 GPIO 引脚上的运动传感器)作为后台进程启动。这 运行 很好,如果检测到它会发送电子邮件。
我正在尝试添加一种方法来播放警笛声(或 100 分贝的黑金属歌曲,尚未决定 :p )。
该方法本身在 class 中并且工作正常,我可以 activate/deactivate 它作为 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
和海妖class相似:
class Siren(Process):
def __init__(self):
super().__init__()
def run(self):
Process.__init__(self)
#code using pyaudio
我猜我对 classes 和方法的非常基本的理解是这里的罪魁祸首。
我也尝试过使用事件,所以我没有 siren.start()
,而是 e = Event()
和 e.set()
,但我无法 运行 Siren class 在后台,让它用 e.wait()
.
来选择那个事件
谁能给我指出正确的方向?
谢谢!
您不应该从 Alarm.run
呼叫 Process.__init__
!您已经在 Alarm.__init__
中这样做了
Process.__init__(self)
与 super().__init__()
完全相同
更新
这有帮助吗:
import threading
import time
class Alarm(threading.Thread):
def run(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
for x in range(20):
print ".",
time.sleep(1)
siren.term()
siren.join()
class Siren(threading.Thread):
def run(self):
self.keepRunning=True;
while self.keepRunning:
print "+",
time.sleep(1)
def term(self):
self.keepRunning=False
if __name__ == '__main__':
alarm = Alarm()
alarm.run()
输出:
+. + . .+ . + +. +. . + . + . + . + + . + . + . + . + . + . + . + . +. +. +
我遇到了一些问题,我的许多 tests/searches 到目前为止都没有带我走。
我在 raspberry pi 上有一个 Flask 应用程序,它为小型报警系统 运行 提供了 GUI。 GUI 允许将警报(目前是 GPIO 引脚上的运动传感器)作为后台进程启动。这 运行 很好,如果检测到它会发送电子邮件。 我正在尝试添加一种方法来播放警笛声(或 100 分贝的黑金属歌曲,尚未决定 :p )。 该方法本身在 class 中并且工作正常,我可以 activate/deactivate 它作为 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
和海妖class相似:
class Siren(Process):
def __init__(self):
super().__init__()
def run(self):
Process.__init__(self)
#code using pyaudio
我猜我对 classes 和方法的非常基本的理解是这里的罪魁祸首。
我也尝试过使用事件,所以我没有 siren.start()
,而是 e = Event()
和 e.set()
,但我无法 运行 Siren class 在后台,让它用 e.wait()
.
谁能给我指出正确的方向?
谢谢!
您不应该从 Alarm.run
呼叫 Process.__init__
!您已经在 Alarm.__init__
Process.__init__(self)
与 super().__init__()
更新
这有帮助吗:
import threading
import time
class Alarm(threading.Thread):
def run(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
for x in range(20):
print ".",
time.sleep(1)
siren.term()
siren.join()
class Siren(threading.Thread):
def run(self):
self.keepRunning=True;
while self.keepRunning:
print "+",
time.sleep(1)
def term(self):
self.keepRunning=False
if __name__ == '__main__':
alarm = Alarm()
alarm.run()
输出:
+. + . .+ . + +. +. . + . + . + . + + . + . + . + . + . + . + . + . +. +. +