I'm completely lost as to how I would write await/async code within a Gtk Application. The example in the README is too short to be of any use, and those in examples/ are also somewhat lacking and very out-of-date.
Here's roughly what I have so far:
async def fetch(session, id):
async with session.get(URL.format(id)) as resp:
return ET.fromstring(await resp.text())
async def update_things(callback):
async with aiohttp.ClientSession() as session:
for r in asyncio.as_completed(tuple(fetch(session, id) for id in THINGS)):
xml = await r
# Process data...
game_callback(ratio)
class AppWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.progressbar = Gtk.ProgressBar()
self.progressbar.show()
self.add(self.progressbar)
await update_things(self.update)
self.progressbar.hide()
def update(self, fraction):
self.progressbar.set_fraction(fraction)
class Application(Gtk.Application):
window = None
def __init__(self, *args, **kwargs):
super().__init__(*args, application_id="foo", **kwargs)
def do_activate(self):
if not self.window:
self.window = AppWindow(application=self, title="Foo")
self.window.present()
if __name__ == "__main__":
gbulb.install(gtk=True)
loop = asyncio.get_event_loop()
loop.run_forever(Application(), sys.argv)
But, I can't use the await update_things() line, as the init function is not async. The closest thing I've been able to find, is changing that to asyncio.ensure_future(update_things()), but the issue is that there doesn't seem to be a way to pause the current code like await does, in order to run the progressbar.hide() line after that coroutine has completed.
I've also tried loop.run_until_complete() and similar functions, but it doesn't seem possible to run another coroutine that way either.
I'm completely lost as to how I would write await/async code within a Gtk Application. The example in the README is too short to be of any use, and those in
examples/are also somewhat lacking and very out-of-date.Here's roughly what I have so far:
But, I can't use the
await update_things()line, as the init function is not async. The closest thing I've been able to find, is changing that toasyncio.ensure_future(update_things()), but the issue is that there doesn't seem to be a way to pause the current code like await does, in order to run theprogressbar.hide()line after that coroutine has completed.I've also tried loop.run_until_complete() and similar functions, but it doesn't seem possible to run another coroutine that way either.