Skip to content

Callback is never called when set_result() is called on a Future #3

@talljosh

Description

@talljosh

When code triggered by a Gtk signal calls set_result() on an asyncio Future object, it seems the callbacks on the Future object are not called unless something else wakes up the main loop.

I've put together the following minimal example of this bug.

Expected behaviour: clicking the button should print, 'button clicked' and 'future finished', then the main loop should end.
Actual behaviour: clicking the button prints 'button clicked', then the main loop continues forever.
Tested with: Python 3.6.9, Gtk 3.22.30 and asyncio-glib 0.1.

If you uncomment the heartbeat line, so that there's a regular timed call happening, then the Future's callback is called and the main loop ends. But with that line commented, the loop does not end.

import asyncio
import asyncio_glib
from gi.repository import Gtk

asyncio.set_event_loop_policy(asyncio_glib.GLibEventLoopPolicy())


class Demo:
    def __init__(self):
        self.future = asyncio.get_event_loop().create_future()

        self.window = Gtk.Window()
        button = Gtk.Button.new_with_label('Click here')
        button.connect('clicked', self.button_clicked)
        self.window.add(button)
        self.window.show_all()

    def button_clicked(self, widget):
        print('button clicked')
        self.window.close()
        self.future.set_result(None)


async def main():
    # Uncomment the following line and everything works.
    # asyncio.get_event_loop().call_later(1, heartbeat)
    demo = Demo()
    await demo.future
    print('future finished')


def heartbeat():
    asyncio.get_event_loop().call_later(1, heartbeat)
    print('tick')


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions