Run code in background indefinitely

This module allows you to keep running a script in the background indefinitely. A great usage of this is fetching data in background and sending notifications with notifications. You can also run a server or a Discord bot for example.

Note: Because of privacy, apps cannot access to the clipboard in background, so coding a clipboard manager is not possible.

class background.BackgroundTask(audio_path: str = None, id: str = None)

Represents a task to run in background. When started, the audio at the path passed to the initializer is played. If no audio is passed, a blank audio is used so Pyto isn’t killed by the system.


import background as bg

with bg.BackgroundTask() as b:
    while True:
execution_time() → int

Returns the total execution time of the task in seconds.

Return type:int
id = None

A string that identifies the task. If a task with the same ID of a task that is already running is started, the other task will be stopped. That is useful with Shortcuts Personal Automations where you can create a background task multiple times to make sure it’s running without having to worry about the task already running.


The delay in seconds since each reminder notification. If set to 3600, a notification will be sent every hour while the task is running. The default value is 21600 (6 hours).

Return type:int

A boolean indicating whether a notification should be sent while the task is running. By default, a notification is sent every 6 hours while the task is running, set this property to False to disable that,

Return type:bool

Starts the background task. After calling this function, Pyto will not be killed by the system.


Stops the background task. After calling this function, Pyto can be killed by the system to free memory (if no other task is running).

wait(delay: float)

Waits n seconds. Does the same thing as time.sleep.

Parameters:delay – Seconds to wait.
exception background.TaskExit

An exception thrown when a task is stopped.


This function is used to start fetching information in background. It tells the system to execute the script from which the function is called multiple times a day. The OS decides when it’s appropiate to perform the fetch, so it’s pretty unreliable. The script cannot take more than 30 seconds to execute.

For example, you can write a script for sending notifications or updating a widget.

import background as bg
import notifications as nc
import widgets as wd
import time


current_time = time.strftime("It is %H:%M")

# Send notification

notif = nc.Notification()
notif.message = current_time

# Update widget

time_label = wd.Text(current_time)

widget = wd.Widget()
for layout in (

wd.save_widget(widget, "Fetching Test")