Search code examples
pythonarchitecturedaemon

Daemon process structure


I am looking to build a daemon process that does some tasks when given some input. 99% of the time it lays silent on the background doing nothing and the tasks are short and few in number. How would I build the interface between two applications one of which constructs the task and the daemon that executes it?

I was thinking that the daemon might have a folder which i periodically checks. If there are some files in there, it reads it and follows instructions from there.

Would that work well or is there a better way?

EDIT: added example daemon code.

#!/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() 

Solution

  • I would look into unix sockets of FIFOs as an option. This eliminates need for polling of some directory. Some SO link for help How to create special files of type socket?