Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at

Quickstart: Background Tasks

If you need to run a process that takes a long time, you want your app to carry on running while it executes.

Follow this quickstart to trigger an artificially slow-running HTTP request in the background, and return control to your main app while it executes.

Create an app

Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.

Location of the Create App button

Add a Server Module

In the App Browser, click the + next to Server Code to add a new Server Module.

Adding a Server Module in the App Browser

You will see a code editor with a yellow background.

Python code with a yellow background

Write a Background Task

Write this function into the Server Module

def make_slow_request():
  response = anvil.http.request("")  # An API that provides slow responses

The @anvil.server.background_task decorator means that this function can be run in the background.

Launch the Background Task

Still in the server module, write a function that launches the Background Task

def launch_slow_request_task():
  task = anvil.server.launch_background_task('make_slow_request')
  return task

The @anvil.server.callable decorator means this function can be called from the client code.

Go to the code for Form1. It looks like this:

Code View for Form1

At the end of the __init__ method, write these lines:

    self.task ='launch_slow_request_task')

This means your function will run when the app starts, and the task object will be stored. The task object holds all available information about the task.

Run your app

Now click the ‘Run’ button at the top of the screen.

Running your app in the editor

The bottom of the Output Panel will show your Background Task running:

Output Panel with one make_slow_request Task running at the bottom, and a Kill button to stop it

Check the output

Click on Background Tasks in the Gear Menu Icon that looks like a cogwheel:

The Gear Menu with Background Tasks highlighted

You’ll see a table showing all the Background Tasks that this app has ever run:

A list of Background Tasks showing each task's status and when it ran

Click on View Logs. You’ll be taken to the App Logs entry for that Task:

The logs for a particular Task, showing when it was launched, the printed data, and when the Task finished

You can see the output from the Background Task. As expected, the Task printed the response from the test HTTP endpoint.

(It’s a Media object with a length of 0 bytes and no content - that’s because the test server ( returns empty responses.)

Copy the example app

Click on the button below to clone a finished version of this app into your account.

Next up

Want more depth on this subject?

You can pass data from a Background Task into the main app, access a task’s state, and terminate it programmatically.

You can also get a list of all the Background Tasks your app has.

Read more about Background Tasks to find out how.

Want another quickstart?

Every quickstart is on the Quickstarts page.