Eel
Eel copied to clipboard
When user closes the window, the program can still be running in the background
Eel version 0.14.0
Describe the bug: When the user is running the app, decides to close the app window GUI, the user believes they have ended the program but the python program is still running in the background and the user has no idea
To Reproduce: Steps to reproduce the behavior:
- create an eel program with an infinite loop
- launch the app and watch GUI run and the python function in the back enter an infinite loop
- then close the app window
- the python program will still be running in the background forever unless the user knows and stops it manually
Expected behavior: I expect both the app GUI and the python program to close when I closed the window
Desktop:
- OS: Windows 11
- Browser: chrome
- Version: Chrome 105.0.5195.102 (Official Build) (64-bit)
Stopping the Eel Server when clients disconnect isn't always wanted behaviour. You can implement the functionality yourself though by calling a python function that stops the server when the website unloads(closes). On JS side it would be:
window.addEventListener("beforeunload", () => { eel.close_python()} );
And in Python:
import sys
@eel.expose
def close_python(*args):
sys.exit()
Hey @nh916, thanks for the question!
I'll start by saying there are a lot of nuances when working with threads... without seeing an exact code example it's hard for me to make recommendations. That said, if you start up additional non-daemon threads and those are still running when the browser is closed, then yeah you're right, the Python application will continue running until those threads are finished.
As @Blauschirm mentioned, halting those threads isn't always wanted because they might be doing a critical bit of work such as saving a file, updating a database, or similar. Therefore, Eel doesn't try to brute force stop them.
You have at least three options here:
- If you're willing to have the additional threads just die off, set
daemon=True
on them and they'll be closed when the main thread is stopped (when the browser is closed) - Similar to what @Blauschirm recommended, Eel provides a
close_callback
that can be passed toeel.start
and do whatever you want to shut down those additional threads (including a brute force exit if you choose) - Follow a pattern similar to what's documented on the Asynchronous Python section of the documentation in the README.md
Regarding recommendation 2 from above... I wouldn't necessarily recommend this, but this is what I mean:
def close(page, sockets_still_open):
os._exit(0)
eel.start('index.html', close_callback=close)
Hope this helps!
Can anyone share a Demo for this behavior, I cannot get this to work.
This can be referenced at - https://github.com/python-eel/Eel/issues/259
Firstly remember to link the eel.js on HTML files can work.
I am faced with this issue as well. Finally found this is the error from eel_init_.py. If import sys
or from sys import exit
is missing in your .py files, this will happen.
But I tried from sys import exit
it still didn't work. I eventually changed eel_init_.py from line 373:
if len(_websockets) == 0:
exit()
to
if len(_websockets) == 0:
sys.exit()
and adding to your .py files:
import sys
then works.