---
title: set_background_task (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/setbackgroundtask-python-agent-api
---

## Syntax

```py
newrelic.agent.set_background_task(flag=True)
```

Marks a transaction as a "background task."

## Description

This call classifies a [transaction](https://docs.newrelic.com/docs/accounts-partnerships/education/getting-started-new-relic/glossary#transaction) as a "background task." Transactions marked as background tasks are visible in the UI as non-web transactions. You can also use this call to reclassify a monitored background task as a web transaction, by passing the `False` parameter.

This API reclassifies an existing transaction as a background task transaction. If there is no existing transaction, this call will not work.

The "background task" designation is typically used for non-web transactions (for example: worker processes, job-based systems, or standalone scripts), but you may also want to designate a web transaction as a "background task" to separate it from your other application transactions. For example, you have a long-running web transaction which is skewing your Apdex score or average response time.

The agent identifies a monitored transaction as a web transaction or background task at the start of the transaction, based on which type of entry-point wrapper initiated the monitoring. To create a background task initially (instead of marking a web transaction as a background task), you would use `background_task`. For a tutorial on how to create background tasks, see [Monitor non-web tasks, scripts, and functions](https://docs.newrelic.com/docs/agents/python-agent/supported-features/python-background-tasks).

> #### 💡 TIP
>
> You can also mark a web transaction as a background task in the WSGI environ dictionary. To do so, set the `newrelic.set_background_task` key for the specific request in the WSGI environ dictionary passed by the WSGI server in your target WSGI app.

## Parameters

| Parameter        | Description                                                                                              |
| ---------------- | -------------------------------------------------------------------------------------------------------- |
| `flag` _boolean_ | Optional. Default value is `True`. Use `False` to instead classify the transaction to a web transaction. |

## Return values

None.

## Examples

### Setting web request to background task [#bg-task-example]

An example of setting a web transaction to a non-web background task:

```py
def wsgi_app(environ, start_response):
    newrelic.agent.set_background_task()
```

### Reclassifying a background task as a web transaction [#bg-task-example-2]

Sometimes, you may want to instead classify a background task as a web transaction, so you can see it with your other application web transactions. To do that, add this call where a monitored background task executes:

```py
import newrelic.agent
newrelic.agent.set_background_task(False)
```
