Skip to content

How to run background tasks

Sometimes you may wish to run arbitrary asyncio tasks in the background of the lightbus run process. You can set these up in your file:

import asyncio
import lightbus

bus = lightbus.create()

async def my_background_task():
    while True:
        await asyncio.sleep(1)

def on_startup(**kwargs):

Important points to note are:

  • The background task will be automatically cancelled when the bus is closed. You can disable this by passing cancel_on_close=False.
  • Any errors in the background task will be bubbled up and cause the Lightbus process to exit. If this is not desired you can implement your own try/except handling within the function being executed.


If you wish to schedule a recurring task then you should probably use @bus.client.every() or @bus.client.schedule(). See how to schedule recurring tasks.