0
import time
import daemon
import sys
import os
import mig_file
class Test:

    def __init__(self):
        self.running = False
        self.listfiles = []

    def run_daemon(self):
        pid = str(os.getpid())
        pidfile_ = '/DAEMON_TEST/versions/files/daemon.pid'
        with open(pidfile_, 'w') as file_:
            file_.write(pid)
        self.worker()

    def worker(self):
        while True:
            if self.listfiles:
                for i in self.listfiles:
                    self.running = True
                    """file operation that read and updates in new output file """
                    mig_file.main_test(i)
                    self.listfiles.remove(i)
            else:
                self.running = False

    def adder(self,files):
        self.listfiles.append(files)

    def main_root(self):
        if len(sys.argv) <=4:
            if sys.argv[1] == 'stop':
                pidfile = '/DAEMON_TEST/versions/files/daemon.pid'
                with open(pidfile, 'r') as file:
                    pid = int(file.read().strip())
                os.kill(pid, 15)  # Send SIGTERM signal to the process with the given PID

            if sys.argv[1] == 'start':
                with daemon.DaemonContext():
                    self.run_daemon()

            if sys.argv[1] == 'add':
                e1 = sys.argv[2]
                self.adder(e1)
                if not self.running:
                    with daemon.DaemonContext():
                        self.run_daemon()

ob =Test()
ob.main_root()

After starting daemon, when I add multiple files via command line argument one by one it is not completing(file operation in worker function) one after the other rather it goes in round robin fashion. my requirement is completing/processing one file after the other. would be a great help if someone could help me out.

develop
  • 55
  • 10
  • 1
    Does this answer your question? [How do you create a daemon in Python?](https://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python) – h4z3 May 23 '23 at 07:36
  • First question: why use forks? I linked a dupe, and in the top answer there's code you use - "Sander Marechal's code sample" originally made in 2004 (and converted to python3 in 2009, according to the link to the snippet itself) - that is "historical solution". "Current solution" in the same answer is python-daemon library (which you accidentally tagged in your question). https://pypi.org/project/python-daemon/ – h4z3 May 23 '23 at 07:43
  • tx @h4z3 i did use the library and modified the above code. Not able to process one file after the other in the above code. – develop May 23 '23 at 11:51
  • @h4z3 seems like file appended is not to reflected in the self.listfiles in daemon process after the daemon is started – develop May 23 '23 at 18:19
  • Yes. Because you do the add command it's in another instance (you start the program again) - so it's not connected with the original one that runs in the background, it has its own memory. Moreover, because `add` checks for `self.running` which is set to False, it will spawn its own daemon. If anything, value of self.running should be dermined on whether pid file exists and the said pid process is alive. For actually sending into to the daemon, you gotta use some interprocess communication - https://docs.python.org/3/library/ipc.html – h4z3 May 24 '23 at 08:24
  • [cont] Hm, lots of that IPC stuff is actually for subprocesses instead of just any other process... The actual non-subprocess stuff are mainly fifo, pipes, and sockets. Sockets seem to have a nice wrapper for other processes - https://stackoverflow.com/questions/6920858/interprocess-communication-in-python (note, this code is for python2, it might be slightly different now). Docs - https://docs.python.org/3/library/multiprocessing.html#multiprocessing-listeners-clients – h4z3 May 24 '23 at 08:29

0 Answers0