Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Helps you write code using gevent

Project Description

greenlight is a powerful decorator that helps you write code using gevent. You can decorate an existing function to run it in a greenlet:

>>> from greenlight import greenlight
>>> from gevent.greenlet import Greenlet
>>> @greenlight
... def async_squaring(n):
...     "An extremely useful function"
...     return n**2
>>> g = async_squaring(10)
>>> isinstance(g, Greenlet)
>>> g.get()

More interestingly, you can chain greenlets inside a greenlit generator (similar to Twisted’s inlineCallbacks). This saves boilerplate; one would otherwise have to call gevent.spawn() on each function, then either link() them together, or, perhaps, call join() or get() on each one and pass the result to the next. With greenlight, you can simply yield to get results inline:

>>> @greenlight
... def square_thrice(n):
...     squared = yield async_squaring(n)
...     tothe4th = yield async_squaring(squared)
...     tothe8th = yield async_squaring(tothe4th)
>>> square_thrice(2).get()

Since a greenlit function always returns a greenlet itself, you can write code very similar to the synchronous code you normally would, while still ensuring execution order, simply by chaining greenlit functions together.

You can explicitly return values from the greenlit generator using the green_return function. If green_return is not used, the result of the last greenlet yielded is returned (as in the previous example):

>>> from greenlight import green_return
>>> @greenlight
... def greater_than_100():
...     a = 2
...     while True:
...         a = yield async_squaring(a)
...         if a>100:
...             green_return(a)
>>> greater_than_100().get()

Error handling works as you would expect:

>>> @greenlight
... def something_bad():
...     raise Exception('O NOES')
>>> @greenlight
... def main():
...     try:
...         hundred = yield async_squaring(10)
...         yield something_bad()
...     except Exception, e:
...         print e
...         green_return(None)
>>> main().get()

greenlight normally behaves like gevent.spawn, in that it starts greenlets for you. If you don’t want that to happen, you can use greenlight_nostart:

>>> from greenlight import greenlight_nostart as greenlight
>>> @greenlight
... def squared(n):
...     return n**2
>>> g = squared(4)
>>> g.started
>>> g.start(); g.get()

Release History

This version
History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(7.0 kB) Copy SHA256 Hash SHA256
Egg 2.6 Oct 17, 2010
(6.9 kB) Copy SHA256 Hash SHA256
Egg 2.7 Oct 17, 2010
(3.3 kB) Copy SHA256 Hash SHA256
Source None Oct 17, 2010

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers