2

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()
1
  • Why don't you use a systemd service? Commented Apr 5, 2019 at 15:29

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

I never tested it with print but I cannot seem to keep a server running using this. My friend suggested me to set the creation flag in the subprocess Popen function to CREATE_NEW_PROCESS_GROUP
It's hard to say what without knowing what the server is doing but assuming it's waiting for input or in a loop this should work.
my script is not executing when I run it using the Process class, whereas it runs fine with the Thread class.I know it is weird but it is true. Is there any way we can execute a funtion in a subprocess.Popen() ? Because after I have compiled my main script into exe I will need something to call that exe file with subprocess.Popen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.