Search code examples
python-3.xmultithreadingprocesssubprocessbackground-process

How can you create a independent process with python running in the background


I am trying to create a independent, behind the scenes process which runs completely on it's own with python. Is it possible to create such a process where even after an exiting the python script the process still keeps running in the background. I have created an .exe file with pyinstaller and I want that file to run in the background without popping up a console such that the user is not aware of the process unless he tends to open his Task Manager quite often.

The multiprocessing module helps to create processes but they are terminated after the script execution is completed. Same with the threading module.

Is it by any way possible to keep some particular piece of code running in the background even after the script has finished execution? or start the whole script in the background altogether without displaying any console?

I have tried using the Process class from the multiprocessing module and the Thread class from the threading module, but all of the exit after script execution. Even the subprocess and os module proves to be of no help

from multiprocessing import Process
from threading import Thread

def bg_func():
   #stuff

def main():
   proc = Process(target=bg_func,args=())  #Thread alternatively
   proc.start()

Solution

  • When I found this problem the only solution I can see is to do a Double Fork. Here is an example that works.

    import os
    import time
    from multiprocessing import Process
    
    
    def bg():
        # ignore first call 
        if os.fork() != 0:
            return
        print('sub process is running')
        time.sleep(5)
        print('sub process finished')
    
    
    if __name__ == '__main__':
        p = Process(target=bg)
        p.start()
        p.join()
        print('exiting main')
        exit(0)
    

    The output shows the subprocess starting, the main program exiting status 0, my prompt coming back before the background program printed the last line. We are running in the background. :D

    [0] 10:58 ~ $ python3 ./scratch_1.py
    exiting main
    sub process is running
    [0] 10:58 ~ $ sub process finished