Hi.
I’m trying to make a simple example on how to make custom non-blocking functions using Gio.Task
and Python.
Code snippet
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gio, GLib, Gtk
def callback(obj, task, *args):
print("Object :", obj)
print("Result :", task.propagate_pointer()) # Error 2
def blocking_func(task, but, prompt, cancellable):
ret = input(prompt)
task.return_pointer(ret, None) # Error 1
def on_clicked(but):
# We will make the text in our entry as prompt for input.
cancellable = None # For sake of readability
prompt = entry.get_text()
task = Gio.Task.new(but, cancellable, callback, prompt)
task.run_in_thread(blocking_func)
win = Gtk.Window()
entry = Gtk.Entry()
button = Gtk.Button(label="Click")
button.connect("clicked", on_clicked)
grid = Gtk.Grid()
grid.add(entry)
grid.add(button)
win.add(grid)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
As easy as that, the idea is to have a window with an entry bar and button. When button is clicked, it will take input from user (from terminal or console) and print it. The prompt for input should not block the GUI, so it should be an asynchronous implementation.
All went well, the app didn’t freeze while there was waiting prompt, but problem came when I needed to return the input of user.
I used task.return_pointer(ret, None)
but I got error :
ValueError: Pointer arguments are restricted to integers, capsules, and None. See: https://bugzilla.gnome.org/show_bug.cgi?id=683599
(The link took me to a bug report of 2012)
Feeling broken, I tested how it works when it returns integer. So I changed the same like to : task.return_pointer(len(ret), None)
. Though it gave proper output but it gave me warning :
** (scratch.py:20816): WARNING **: 00:07:39.748: (gi/pygi-basictype.c:78):marshal_from_py_void: runtime check failed: (arg_cache->transfer == GI_TRANSFER_NOTHING)
What am I doing wrong? What is the proper way to return a value in Gio.Task
?
Thanks !