0

I have this test code in python :

import os
from multiprocessing import Process

import time
def testfunc():
  os.setsid()
  time.sleep(60)

p = Process(target=testfunc, args=())
p.daemon=tru
p.start()
#p.join()
#print(p)
print(p.ident)
exit()

and when i run the program and stop the parent process, the child process stop tow. so is there any way to make processes not die after its parent exit without using fork method ?

Hayder Ctee
  • 145
  • 9

1 Answers1

0

The documentation states -

When a process exits, it attempts to terminate all of its daemonic child processes.

You probably need something like this (example from the link) -

with open(os.devnull, 'r+b', 0) as DEVNULL:
    p = Popen(['/usr/bin/python25', '/path/to/long_process.py'],
              stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT, close_fds=True)
time.sleep(1) # give it a second to launch
if p.poll(): # the process already finished and it has nonzero exit code
    sys.exit(p.returncode)

which will spawn you an independent process. Also, see python-daemon package.

Sushant
  • 3,499
  • 3
  • 17
  • 34
  • If the OP is using a UNIX-like system (which seems likely, since `os.setsid()` only exists on UNIX-like systems), I would definitely make python-daemon the primary suggestion. – Roland Smith Jun 02 '19 at 20:54