Skip to main content

Trio guest mode wrapper for PyGTK

Project description

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. This function must accept a nursery argument which can spawn child tasks for the duration of the host loop.

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(nursery):
    Example(nursery)


trio_gtk.run(main)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trio-gtk-0.1.1.tar.gz (15.1 kB view hashes)

Uploaded Source

Built Distribution

trio_gtk-0.1.1-py3-none-any.whl (14.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page