9

I would like to create a process using the mutliprocessing module in python but ensure it continues running after the process that created the subprocess exits.

I can get the required functionality using the subprocess module and Popen, but I want to run my code as a function, not as a script. The reason I want to do this is to simplify creating pyro (python remote objects) objects. I want to start the pyro object request handler in a separate process using multiprocessing, but then I want the main process to exit while the process supporting the pyro object continues to run.

Sheena
  • 13,328
  • 11
  • 65
  • 103
glenn
  • 131
  • 1
  • 4
  • here is another way to detach a process that is started using `multiprocessing` https://stackoverflow.com/questions/49123439/python-how-to-run-process-in-detached-mode – Trevor Boyd Smith Jul 09 '19 at 20:02

2 Answers2

4

I finally got what I wanted. I appreciate any suggestions to improve the code.

def start_server():
    pyrodaemon = Pyro.core.Daemon()
    #setup daemon and nameserver
    #Don't want to close the pyro socket
    #Need to remove SIGTERM map so Processing doesn't kill the subprocess
    #Need to explicitly detach for some reason I don't understand
    with daemon.DaemonContext(files_preserve=[pyrodaemon.sock],signal_map={signal.SIGTERM:None},detach_process=True):
        while running:
            pyrodaemon.handleRequests(timeout=1.0)
    #when finished, clean up
    pyrodaemon.shutdown()

def main():
    p = Process(target=start_server)
    p.daemon=True # Need to inform Process that this should run as a daemon
    p.start()
    time.sleep(3.0) # Important when running this program stand alone: Must wait long enough for start_server to get into the daemon context before the main program exits or Process will take down the subprocess before it detaches
    do_other_stuff_not_in_the_daemon()
glenn
  • 131
  • 1
  • 4
  • 1
    The daemon word is getting abused here ;) *Don't* set Process.daemon to True. That tells multiprocessing to attempt to kill the child at exit (confusing huh?). I think this is why you're needing to catch SIGTERM and set detach_process in the above code. - http://docs.python.org/library/multiprocessing.html#multiprocessing.Process.daemon – JimB Nov 19 '09 at 14:59
0

What you're trying to do is start a daemon process. Look at PEP-3143, and the python-daemon package.

Took a look into Pyro, and it seems they include their own daemonzing module,

Pyro/ext/daemonizer.py
JimB
  • 87,033
  • 9
  • 198
  • 196
  • Thank you for the reply. Having come across that solution myself recently, I agree. However, python-daemon seems to be targeted to the more standard paradigm where a program has one function that is started as a daemon and never returns. I would like to do something along the lines of: with daemon.DaemonContext(): some_daemon_loop() continue_with_this_function_after_daemon_has_launched() I appreciate any suggestions. – glenn Nov 19 '09 at 06:47
  • I hadn't really looked at pyro before. From a cursory glance at their code, the "daemon" class is something like a threaded dispatcher, and not a separate process; no where in the code does it ever fork. Pyro has a module included for daemon real daemon functionality. – JimB Nov 19 '09 at 14:40