1
我是Python新手。 我有一个线程回调代码在我的raspi上正常工作。Python:线程回调不能用守护进程模式?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import time
from daemon import runner
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # float switch goes down (closed to open) => low water level
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) # float switch goes up (opened to close) => high water level
def callback_lowlevel(channel):
if GPIO.input(channel):
print "Low water level in the sump detected"
else:
print "Water level in the sump returned to normal"
def callback_highlevel(channel):
if GPIO.input(channel):
print "High water level in the sump detected"
else:
print "Water level in the sump returned to normal"
GPIO.add_event_detect(23, GPIO.BOTH, callback=callback_lowlevel, bouncetime=1000)
GPIO.add_event_detect(24, GPIO.BOTH, callback=callback_highlevel, bouncetime=1000)
如果我开始一个无限循环这样的:
try:
print "Waiting for events"
while True:
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
它的工作原理。
但是,如果我用守护进程“守护进程”它,我的线程回调不再工作。
class App(): # Daemon content, not doing much, sleeping mostly, to lower CPU footprint
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/stdout'
self.stderr_path = '/dev/stdout'
self.pidfile_path = '/var/run/aquamonitor.pid'
self.pidfile_timeout = 5
def run(self):
Logger("Starting monitoring")
while True:
time.sleep(1) # Sleep most of time to be not too CPU intensive
app = App() # Init the App
daemon_runner = runner.DaemonRunner(app) # Run as a daemon
daemon_runner.do_action() # Just do it
我在做什么错?它是一个守护进程的事实改变了我应该写我的线程回调的方式吗?
您是否尝试其他方法来守护进程?......例如'os.fork' –
是否通过这个'self.pidfile_path ='/ var/run/aquamonitor.pid'来守护进程? –
嗨,对于迟到的回答感到抱歉。不,我只是尝试守护进程库。守护进程本身由daemon_runner踢。 – kameo