Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown #83

Closed
Avriox opened this issue Sep 9, 2022 · 8 comments
Closed

Graceful shutdown #83

Avriox opened this issue Sep 9, 2022 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@Avriox
Copy link

Avriox commented Sep 9, 2022

Is there any way of gracefully shutting down the server after use? I am using it to have a configuration page for a larger project and once the configuration is completed the main part of the project takes over and no more configuration is needed. I would like to kill everything UI related at that point.

@rodja rodja self-assigned this Sep 9, 2022
@rodja
Copy link
Member

rodja commented Sep 9, 2022

NiceGUI is using uvicorn as a server which has still problems with graceful shutdowns (see encode/uvicorn#1103 for example). We need to think about this a bit. My not so graceful suggestion would be to start NiceGUI without reload=True, launch a new process on exit and just live with exceptions printed on the console:

from nicegui import ui
import sys

def apply():
    print('Starting main system in seperate process')
    # subprocess.Popen(['python3', 'my_main_system.py'])
    sys.exit(0)


ui.label('Configuration')
ui.label('... your config UI here ...')
ui.button('Apply', on_click=apply)

ui.run(reload=False)

@falkoschindler
Copy link
Contributor

Yes, it would be great to have something like

from nicegui import ui

ui.label('Your configurtion UI...')

def quit():
    ... # How?

ui.button('Quit', on_click=quit)

ui.run(reload=False)

print('Do something else...')

It's a pity that uvicorn.run() starts a server that's not exposed to the outside, but would have a server.shutdown() method.

Monkey-patching uvicorn.run() (or simply copying its content over into our run.py) would be an option. But due to the many configuration options thats more than 120 lines of code and doesn't feel quite right.

@falkoschindler falkoschindler added the enhancement New feature or request label Sep 9, 2022
@rodja
Copy link
Member

rodja commented Sep 13, 2022

The solution multiprocessing.Process-Solution described in https://stackoverflow.com/a/70980373/364388 looked promising. But the whole app needs to be pickled... which does not work in NiceGUI:

AttributeError: Can't pickle local object 'CustomView.use.<locals>.<lambda>'

I've pushed the code to branch #83-Process.

@rodja
Copy link
Member

rodja commented Sep 13, 2022

I've got a first idea working on #83-ui.shutdown.
The main idea stems from @falkoschindler #83 (comment). We could simply create the uvicorn server object inside nicegui instead of relying on the uvicorn.run helper. The API may look as following:

ui.button('Quit', on_click=ui.shutdown)

But there is still work to be done. For example auto-reloading is still broken.

@falkoschindler
Copy link
Contributor

I tried to add more code from uvicorn.run: 501323d

Now the app starts with reload=False and reload=True. But with auto-reload the "Quit" button does not work, because the spawned process has no nicegui.globals.server set. That's only the case in the parent process. But maybe we don't need reloading anyway. We could simply raise an exception that ui.shutdown does not work with reload=True.

Another issue: When the socket connection is lost due to the server being shut down, the JustPy frontend asks for confirmation to reload the page. This makes no sense and should be avoided.

@falkoschindler
Copy link
Contributor

Nice! After moving ui.shutdown into the lifecycle module the reload-confirmation dialog is gone.

@rodja rodja pinned this issue Sep 13, 2022
@rodja
Copy link
Member

rodja commented Sep 15, 2022

The ui.shutdown() method is now available in v0.8.16

@rodja rodja closed this as completed Sep 15, 2022
@rodja rodja unpinned this issue Oct 8, 2022
@canDry
Copy link

canDry commented Jan 18, 2023

ui.shutdown

Correction: app.shutdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants