Trio guest mode wrapper for PyGTK
This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
2020-09-21 00:09:33 +02:00
.drone.yml Add missing package 2020-09-20 23:27:32 +02:00
.gitignore Bootstrapping an initial stab at trio-gtk 2020-09-20 23:22:18 +02:00
CHANGELOG.md Remove mandatory nursery argument 2020-09-21 00:03:42 +02:00
LICENSE Bootstrapping an initial stab at trio-gtk 2020-09-20 23:22:18 +02:00
poetry.lock Bootstrapping an initial stab at trio-gtk 2020-09-20 23:22:18 +02:00
pyproject.toml Remove mandatory nursery argument 2020-09-21 00:03:42 +02:00
README.md Fix badge 2020-09-21 00:09:33 +02:00
trio_gtk.py Remove mandatory nursery argument 2020-09-21 00:03:42 +02:00

trio-gtk

Build Status

Trio guest mode wrapper for PyGTK

Using the Trio guest mode feature, we can run both the Trio and PyGTK event loops alongside each other in a single program. This allows us to make use of the Trio library and the usual async/await syntax and not have to directly manage thread pools. This library provides a thin wrapper for initialising the guest mode and exposes a single public API function, trio_gtk.run into which you can pass your Trio main function.

Install

$ pip install trio-gtk

Example

import gi
import trio

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk as gtk

import trio_gtk


class Example(gtk.Window):
    def __init__(self, nursery):
        gtk.Window.__init__(self, title="Example")

        self.button = gtk.Button(label="Create a task")
        self.button.connect("clicked", self.on_click)
        self.add(self.button)

        self.counter = 0
        self.nursery = nursery

        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def on_click(self, widget):
        self.counter += 1
        self.nursery.start_soon(self.say_hi, self.counter)

    async def say_hi(self, count):
        while True:
            await trio.sleep(1)
            print(f"hi from task {count}")


async def main():
    async with trio.open_nursery() as nursery:
        Example(nursery)
        await trio.sleep_forever()


trio_gtk.run(main)