Eel: Restart the server automatically when it shuts off

Created on 25 May 2020  路  3Comments  路  Source: ChrisKnott/Eel

The problem
Hi,

I'm having an issue where the server suddenly disconnects, and for some reason this happens a lot. This forces me to restart the server and open a new window every time, and of course that's not ideal for the users. So I was wondering if there is a way to use the close_callback to restart the server without having to open a new window every time.

I tried the following code and it successfully restarts the server, but it opens a new window every time a redirecting button/link is clicked even if the server is still running.

import eel

eel.init('UI')

def callback(path, websockets):
        eel.start(path, port=5002, close_callback=callback)

eel.start('/Templates/admin/employees.html', port=5002, close_callback=callback)

And if I set the 'mode' argument to False, then the server restarts only if it is closed first, but the current window won't be connected to it, and I have to open a new window to connect to the server.

Desktop:

  • OS: Windows
  • Browser: Chrome - v81.0.4044.138 (Official Build) (64-bit)
  • Eel version: 0.12.3
help wanted

Most helpful comment

I'm experiencing similar issues in development mode. I'm using React for the front-end, in case that matters.

I'm not sure why it happens so frequently that Eel stops running and would love to know how I can better debug.

@mrchrishahn I have experienced a similar issue while developing an Eel app with an Angular frontend, which is similar (I guess) to what you are trying to achieve.

The problem is that Eel, by default, exits when all of its websockets are closed (see below). This behaviour is needed in production, because we want our app to exit whenever the browser window is closed.

https://github.com/samuelhwilliams/Eel/blob/c0f26528ac32f3b2f02da2c74d286e5fe4a3af27/eel/__init__.py#L358-L370

While developing with Angular, each time a file is saved, the open web page is automatically reloaded to display the new changes. Therefore, while reloading, the socket is closed, and if a new one is not reopened in less than one second, Eel exits. (I don't have any experience with React, but I think apps initialized with create-react-app also have an autoreload feature.)

The workaround I've used :

    if develop:
        directory = 'src'
        app = None
        page = {'port': 3000}
        def close_callback(page, sockets):
            pass
        print("Starting eel")
    else:
        directory = 'build'
        app = 'chrome-app'
        page = 'index.html'
        close_callback = None

    # ...

    eel.start(page, mode=app, close_callback=close_callback, **eel_kwargs)

If develop is True, we simply override the close callback to do nothing.

All 3 comments

Hi @abdurahman-shiine - can you share your code? Maybe we could help you work out what the problem is and why your server is shutting down so often.

I can see why it would be annoying and why you'd want some sort of restart functionality. I'd probably lean towards adding a configuration option that restarts the Eel server or shutdown rather than use a close callback. This is certainly something I'm open to discussing further.

I'm experiencing similar issues in development mode. I'm using React for the front-end, in case that matters.

I'm not sure why it happens so frequently that Eel stops running and would love to know how I can better debug.

This is the start function (copy of the example file)

def start_eel(develop):
    """Start Eel with either production or development configuration."""

    if develop:
        directory = 'src'
        app = None
        page = {'port': 3000}
        print("Starting eel")
    else:
        directory = 'build'
        app = 'chrome-app'
        page = 'index.html'

    eel.init(directory, ['.tsx', '.ts', '.jsx', '.js', '.html'])


    eel_kwargs = dict(
        host='localhost',
        port=8080,
        size=(1280, 800),
    )
    try:
        eel.start(page, mode=app, **eel_kwargs)
        print("Started eel")
    except EnvironmentError as e:
        # If Chrome isn't found, fallback to Microsoft Edge on Win10 or greater
        print("Environment Error", e)
        if sys.platform in ['win32', 'win64'] and int(platform.release()) >= 10:
            eel.start(page, mode='edge', **eel_kwargs)
        else:
            raise e

I'm experiencing similar issues in development mode. I'm using React for the front-end, in case that matters.

I'm not sure why it happens so frequently that Eel stops running and would love to know how I can better debug.

@mrchrishahn I have experienced a similar issue while developing an Eel app with an Angular frontend, which is similar (I guess) to what you are trying to achieve.

The problem is that Eel, by default, exits when all of its websockets are closed (see below). This behaviour is needed in production, because we want our app to exit whenever the browser window is closed.

https://github.com/samuelhwilliams/Eel/blob/c0f26528ac32f3b2f02da2c74d286e5fe4a3af27/eel/__init__.py#L358-L370

While developing with Angular, each time a file is saved, the open web page is automatically reloaded to display the new changes. Therefore, while reloading, the socket is closed, and if a new one is not reopened in less than one second, Eel exits. (I don't have any experience with React, but I think apps initialized with create-react-app also have an autoreload feature.)

The workaround I've used :

    if develop:
        directory = 'src'
        app = None
        page = {'port': 3000}
        def close_callback(page, sockets):
            pass
        print("Starting eel")
    else:
        directory = 'build'
        app = 'chrome-app'
        page = 'index.html'
        close_callback = None

    # ...

    eel.start(page, mode=app, close_callback=close_callback, **eel_kwargs)

If develop is True, we simply override the close callback to do nothing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

datainvestor picture datainvestor  路  5Comments

logxseven picture logxseven  路  6Comments

Swicegood picture Swicegood  路  4Comments

04fsnape picture 04fsnape  路  4Comments

ghost picture ghost  路  6Comments