python how to run process in detached mode

multiprocessing, python

Solution

Python will not end if there exists a non-daemon process.

By setting, `daemon` attribute before `start()` call, you can make the process daemonic.

p = Process(target=func)
p.daemon = True  # <-----
p.start()
print('done')

NOTE: There will be no `sub process finished` message printed; because the main process will terminate sub-process at exit. This may not be what you want.

You should do double-fork:

import os
import time
from multiprocessing import Process


def func():
    if os.fork() != 0:  # <--
        return          # <--
    print('sub process is running')
    time.sleep(5)
    print('sub process finished')


if __name__ == '__main__':
    p = Process(target=func)
    p.start()
    p.join()
    print('done')

Problem

here is a example: ``` from multiprocessing import Process import time def func(): print('sub process is running') time.sleep(5) print('sub process finished') if __name__ == '__main__': p = Process(target=func) p.start() print('done') ``` what I expect is that the main process will terminate right after it start a subprocess. But after printing out 'done', the terminal is still waiting....Is there any way to do this so that the main process will exit right after printing out 'done', instead of waiting for subprocess? I'm confused here because I'm not calling `p.join()`

Original source