Skip to content

Best Practice for Running Background Tasks on Page Load #4053

Answered by rodja
patrickwasp asked this question in Q&A
Discussion options

You must be logged in to vote

There is an undocumented module background_tasks in NiceGUI:

from nicegui import ui, background_tasks

def heavy_computation():
    time.sleep(2)

@ui.page('/')
def index():
    async def compute():
        await run.cpu_bound(heavy_computation)
        spinner.visible = False

    spinner = ui.spinner()
    background_tasks.create(compute())

ui.run()

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by patrickwasp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants