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.
BackgroundTask(audio_path: 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: print(b.execution_time()) b.wait(1)
execution_time() → int¶
Returns the total execution time of the task in seconds.
Return type: int
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
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
Falseto 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.
Waits n seconds. Does the same thing as
Parameters: delay – Seconds to wait.
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")