Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

No obvious way to await till idle #44

Description

@Dreamsorcerer

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationAn improvement required in the project's documentation.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions