background

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.

Usage:

import background as bg

with bg.BackgroundTask() as b:
    while True:
        print(b.execution_time())
        b.wait(1)
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.

notification_delay

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
reminder_notifications

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
start()

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

stop()

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.

background.request_background_fetch()

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

bg.request_background_fetch()

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

# Send notification

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

# Update widget

time_label = wd.Text(current_time)

widget = wd.Widget()
for layout in (
    widget.small_layout,
    widget.medium_layout,
    widget.large_layout):
        
        layout.add_row([time_label])

wd.save_widget(widget, "Fetching Test")