I can’t find GTask interface for Python in either Fedora 31 builds and in flatpak org.gnome.Platform builds. This makes building async based application really hard to do. Am I missing something here? Or it is just a bad state of python bindings? Should I create bug report for this issue?
I want to use Gio.Task for async calls with Python and Gtk Widgets.
You haven’t told us how you’re trying to use GTask.
Anyway, GTask is part of GIO, so you need something like this (WARNING: untested code):
class Foo (GObject.Object):
def __init__(self):
GObject.Object.__init__(self)
def bar(self, question):
'''
@question: the question to ask
Computes the answer synchronously.
'''
# long running computation
return 42
def bar_async(self, question, cancellable, callback):
'''
@question: the question to ask
@cancellable: a Gio.Cancellable, or None
@callback: (callable): a function to be called when the result is
available; takes two arguments: callback(self, result)
Runs bar() asynchronously.
'''
def bar_in_thread(task, self, data, cancellable):
res = self.bar(data.get('question'))
task.return_int(res)
# Pass the arguments for bar as data to the task
task_data = { 'question': question }
task = Gio.Task.new(self, cancellable, callback)
task.set_task_data(task_data)
task.run_in_thread(bar_in_thread)
def bar_finish(self, result):
"""
To be called in the callback passed to bar_async(); result
is the asynchronous result of the callback
"""
return result.propagate_int()