Coder Social home page Coder Social logo

cringe's Introduction

CoRoutines In Nice Gnome Environment

This is basically gevent, but using the GNOME platform libraries.

GLib based API's often have asynchronous versions. These tend to use the Gio pattern which looks something like this:

from gi.repository import GLib, Gio

def _loaded(f, result, user_data):
    success, contents, etag = f.load_contents_finish(result)
    print len(contents)

def _load_stuff(...):
    f = Gio.File.new_for_uri(uri)
    f.load_contents_async(None, _loaded, None)

GLib.idle_add(_load_stuff)
loop = GLib.MainLoop()
loop.run()

But with a sprinkling of greenlet it looks like this:

from cringe import patch_all()
patch_all()
from gi.repository import GLib, Gio

def _load_stuff(...):
    f = Gio.File.new_for_uri(uri)
    contents = f.load_contents()
    print len(contents)

GLib.idle_add(_load_stuff)
GLib.main()

This is still asynchronous! The cringe patch modifies the gi generated bindings to pause the currently running greenlet when it is waiting for an async operation to complete. Control can then return to the mainloop, which can process other incoming events.

Implementation

All Gio-style async API's have an _async and _finish method. The _async function takes a callback and user_data parameter, and the callback gets passed a AsyncResult which can be used to collect the actual return value.

Wrapping this in Greenlet magic gives us:

def function(*args, **kwargs):
    current = greenlet.getcurrent()

    # Call the function and pass it current.switch as the callback - this
    # is what allows the current coroutine to be resumed
    args = args + (current.switch, None)
    some_api_async(*args, **kwargs)

    # Pause the current coroutine. It will be resumed here when the
    # callback calls current.switch()
    obj, result, _ = current.parent.switch()

    # Actually return the expected value
    return some_api_finish(result)

The GLib API mostly followed the same pattern for all of its asynchronous code. This means we can easily apply this pattern to all bindings automagically by hooking in to GObjectMeta (in gi.types).

We monkey-patch in our GreenletMeta which uses heuristics to patch the async API's it can recognise.

cringe's People

Stargazers

 avatar

Watchers

 avatar  avatar  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.