Skip to content

run async function in an endless loop #3067

Answered by falkoschindler
v479038280 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @v479038280,

You can start any async function using app.on_startup:

async def loop():
    while True:
        await async_task()

app.on_startup(loop)

You could also use a ui.timer. Note that this acts like a UI element: When used in global scope (i.e. on the auto-index page), it runs immediately. But when used in a @page function, it only runs when somebody visits that page:

ui.timer(1.0, async_task)  # runs immediately as part of the auto-index page

@ui.page('/timer')
async def timer_page():
    ui.timer(1.0, async_task)  # runs per client connection on /timer

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by v479038280
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