【问题标题】:Python : starting background process from background processPython:从后台进程启动后台进程
【发布时间】: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


    【解决方案1】:

    您不应该从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()
    

    输出:

    +. + . .+ . + +. +. . + . + . + . + + . + . + . + . + . + . + . + . +. +. +
    

    【讨论】:

    • 好的,这是一个错误。我从两个班级中删除了Process.__init__。然而,行为并没有改变。当siren被Alarm进程本身触发时,siren.is_alive()返回True,但实际上并没有执行。它只是转到下一条语句,没有任何错误或警告。
    • @Jackson_DKMG,我添加了一些使用 threading.Thread 的代码,只是因为我习惯这样做。它在你的 Flask 环境中工作吗?
    • 谢谢,它可能真的有帮助。我会在周末进行测试!
    • 其实;这个新问题超出了最初问题的范围。我会将您的答案标记为有效的解决方案,如果我真的无法让它发挥作用,我会打开一个新主题。谢谢!
    • (我上面的评论无关,我删除了)
    最近更新 更多