0

I have a python script which attempts to communicate with a python daemon. When the original script is invoked, it checks to see if the daemon exists. If the daemon exists, the original script writes to a named pipe to communicate with the daemon. If the daemon does not exists, the original script attempts to create a daemon using DaemonContext and then writes to the named pipe.

Pseudo-code of the original script:

from daemon import DaemonContext

if daemon_exists():
    pass
else:
    with DaemonContext():
        create_daemon()

communicate_with_daemon()

The problem is that when the daemon is created, the parent process is killed (i.e. communicate_with_daemon will never be executed). This prevents the original script from creating a daemon and communicating with it.

According to this answer, this problem is a limitation of the python-daemon library. How would I get around this?

Thanks.

Cat
  • 55
  • 8
  • 1
    daemonization is double forking and closing connections to terminals. You could implement this yourself, but I don't recommend it. Why not write a separate client process to handle the daemon communication? – Keozon Mar 27 '18 at 18:28
  • Yeah, that seems like the most sensible way to avoid the issue. Thanks! – Cat Mar 27 '18 at 18:58
  • @Keozon Can you post your comment as an answer? Thanks! – alex Nov 01 '18 at 19:47

1 Answers1

0

You're describing not a limitation, but a definition of how a daemon process works.

[…] the parent process is killed (i.e. communicate_with_daemon will never be executed).

Yes, that's right; the daemon process detaches from what started it. That's what makes the process a daemon.

However, this statement is not true:

This prevents the original script from creating a daemon and communicating with it.

There are numerous other ways to communicate between processes. The general name for this is Inter-Process Communication. The solutions are many, and which you choose depends on the constraints of your application.

For example, you could open a socket at a known path and preserve that open file; you could open a network port and communicate through the loopback interface; you could make a "drop-box" communication at a file on the local filesystem store, a database, or otherwise; etc.

bignose
  • 30,281
  • 14
  • 77
  • 110