GithubHelp home page GithubHelp logo

gbulb's Introduction

gbulb - a PEP 3156 event loop based on GLib

Gbulb is a Python library that implements a PEP 3156 interface for the GLib main event loop under UNIX-like systems.

The code needs to be thoroughly tested, it should be considered as unstable for the moment.

Anthony Baire

Licence

Apache 2.0

Homepage

https://github.com/nathan-hoad/gbulb

Requirements

  • python3.4+ or python3.3 with asyncio
  • pygobject
  • glib
  • gtk+3 (optional)

Usage

GLib event loop

    import asyncio, gbulb
    gbulb.install()
    asyncio.get_event_loop().run_forever()

Gtk+ event loop (suitable for GTK+ applications)

    import asyncio, gbulb
    gbulb.install(gtk=True)
    asyncio.get_event_loop().run_forever()

GApplication/GtkApplication event loop

    import asyncio, gbulb
    gbulb.install(gtk=True)  # only necessary if you're using GtkApplication

    loop = asyncio.get_event_loop()
    loop.run_forever(application=my_gapplication_object)

Known issues

  • Windows is not supported, sorry. If you are interested in this, please help me get it working! I don't have Windows so I can't test it.

Divergences with PEP 3156

In GLib, the concept of event loop is split in two classes: GLib.MainContext and GLib.MainLoop.

The event loop is mostly implemented by MainContext. MainLoop is just a wrapper that implements the run() and quit() functions. MainLoop.run() atomically acquires a MainContext and repeatedly calls MainContext.iteration() until MainLoop.quit() is called.

A MainContext is not bound to a particular thread, however it cannot be used by multiple threads concurrently. If the context is owned by another thread, then MainLoop.run() will block until the context is released by the other thread.

MainLoop.run() may be called recursively by the same thread (this is mainly used for implementing modal dialogs in Gtk).

The issue: given a context, GLib provides no ways to know if there is an existing event loop running for that context. It implies the following divergences with PEP 3156:

  • .run_forever() and .run_until_complete() are not guaranteed to run immediately. If the context is owned by another thread, then they will block until the context is released by the other thread.

  • .stop() is relevant only when the currently running Glib.MainLoop object was created by this asyncio object (i.e. by calling .run_forever() or .run_until_complete()). The event loop will quit only when it regains control of the context. This can happen in two cases:

    1. when multiple event loop are enclosed (by creating new MainLoop objects and calling .run() recursively)
    2. when the event loop has not even yet started because it is still trying to acquire the context

It would be wiser not to use any recursion at all. GLibEventLoop will actually prevent you from doing that (in accordance with PEP 3156), however GtkEventLoop will allow you to call run() recursively. You should also keep in mind that enclosed loops may be started at any time by third-party code calling GLib's primitives.

gbulb's People

Contributors

a-ba avatar a-baire avatar jluebbe avatar pocek avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.