I am looking for a way for my application to start its background logging application as a completely separate process.
The program is 90% user input so crashes happen, I need to see the logs but when using subprocess
it spawns as a child process which means when the main application crashes so does the logger.
I have tried using os.system
but that launches it as part of the current process stopping all execution until the process has completed.
The Logger accepts 3 arguments, a temp folder path, a port and a process id. Theses are required to enable the program to communicate to the main application.
Both applications are frozen and build on windows explicitly.
There is not much code I can show but here is how everything is generated.
if getattr(sys, 'frozen', False):
debug = False
exe_path = os.path.dirname(sys.executable)
elif __file__:
debug = True
exe_path = os.path.dirname(os.path.abspath(__file__))
def get_open_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 0))
addr = s.getsockname()
open_port = addr[1]
s.close()
return open_port
proc = [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if str(os.getpid()) in str(p.info['pid'])][0]
proc = proc['name']
tmp = tempfile.mkdtemp()
tmp = str(tmp)+"\\"
port = get_open_port()
path = os.path.join(exe_path, "logger", "Logger.exe")
#os.startfile('''%s", "%s", "%s", "%s"''' %(path, tmp, proc, port)) #failes to get past here.
#subprocess.Popen([path, tmp, proc, port]) #creates as a child process which crashes with the main.
time.sleep(2) # waits for the application to start