守护进程结构
Daemon process structure
我希望构建一个守护进程,它在给定一些输入时执行一些任务。 99% 的时间它在后台保持沉默,什么都不做,任务很短,数量也很少。我将如何构建两个应用程序之间的接口,其中一个构建任务和执行任务的守护进程?
我在想守护进程可能有一个我定期检查的文件夹。如果那里有一些文件,它会读取它并按照那里的说明进行操作。
这样好还是有更好的方法?
编辑:添加示例守护程序代码。
#!/usr/bin/python
import time
from daemon import runner
class Daemon():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
self.task_dir = os.path.expanduser("~/.todo/daemon_tasks/")
def run(self):
while not time.sleep(1):
if len(os.listdir(self.task_dir)) == 0:
for task in os.listdir(self.task_dir):
self.process_task(task)
def process_task(self, task):
# input: filename
# output: void
# takes task and executes it according to instructions in the file
pass
if __name__ == '__main__':
app = Daemon()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
我会考虑将 FIFO 的 unix 套接字作为一个选项。这消除了对某些目录进行轮询的需要。一些 SO link 寻求帮助 How to create special files of type socket?
我希望构建一个守护进程,它在给定一些输入时执行一些任务。 99% 的时间它在后台保持沉默,什么都不做,任务很短,数量也很少。我将如何构建两个应用程序之间的接口,其中一个构建任务和执行任务的守护进程?
我在想守护进程可能有一个我定期检查的文件夹。如果那里有一些文件,它会读取它并按照那里的说明进行操作。
这样好还是有更好的方法?
编辑:添加示例守护程序代码。
#!/usr/bin/python
import time
from daemon import runner
class Daemon():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
self.task_dir = os.path.expanduser("~/.todo/daemon_tasks/")
def run(self):
while not time.sleep(1):
if len(os.listdir(self.task_dir)) == 0:
for task in os.listdir(self.task_dir):
self.process_task(task)
def process_task(self, task):
# input: filename
# output: void
# takes task and executes it according to instructions in the file
pass
if __name__ == '__main__':
app = Daemon()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
我会考虑将 FIFO 的 unix 套接字作为一个选项。这消除了对某些目录进行轮询的需要。一些 SO link 寻求帮助 How to create special files of type socket?