from gi.repository import GObject def my_callback(data): print(data) main_context = GObject.main_context_default() # Add a task to the main context GObject.idle_add(my_callback, "Hello World") # Run the main context while main_context.pending(): main_context.iteration()
from gi.repository import GObject class MyTask(): def __init__(self, data): self.data = data def run(self): print(self.data) main_context = GObject.main_context_default() # Add a task to the main context task = MyTask("Hello World") GObject.idle_add(task.run) # Run the main context while main_context.pending(): main_context.iteration()This code defines a class called MyTask that has a run method. The run method just prints some data to the console. Then, a task object is created with some data and added to the main context as an idle task. Finally, the main context is run in a loop until there are no more pending tasks. In both examples, the package library used is GObject, which is part of the gi.repository package in Python.