[Python] python exe keeps running after calling os.kill and sys.exit(0)

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I implemented a web app for visualizing test data to charts. When the app starts to run, it opens a tab in the browser and shows data. I build .exe with parameters --windowed for not having console popup.

    Problem :

    After starting to run the app in task management there is one tab process and two python exe processes. When i clicked closing the tab, then the tab process terminates, but two python exe keep in the task manager. When i run the app again, then in task management there will be 4 python exe, etc etc.

    I tried following, none worked:

    Code :

    app = Flask(__name__,
    template_folder=os.path.join(BASE_DIR, 'templates'),
    static_folder=os.path.join(BASE_DIR, 'static'))
    app.config['DEBUG'] = False # Enable debug mode for detailed error logs

    if __name__ == '__main__':
    # Automatically open the browser
    webbrowser.open("http://127.0.0.1:5000")
    app.run(debug=False, use_reloader=False)


    Method A:

    def shutdown(): print("Received shutdown request. Stopping application...")
    # Get the current process ID
    current_pid = os.getpid()

    # Run Windows taskkill command to forcefully kill the EXE
    subprocess.run(f'taskkill /F /PID {current_pid}', shell=True)

    sys.exit(0) # Ensure full exit


    Method B:

    # Kill the main process
    os.kill(os.getpid(), signal.SIGTERM)
    sys.exit(0) # Ensure full exit


    Method C:

    func = request.environ.get("werkzeug.server.shutdown")
    if func is None:
    raise RuntimeError("Not running with the Werkzeug Server")
    func()


    Expected:

    What I want is that when closing the tab then tab process and two python exe processes are all terminated, the memory is cleared up.

    Continue reading...

Compartilhe esta Página