Skip to main content

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)
True
>>> g.get()
100

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()
256

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()
256

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()
O NOES

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
False
>>> g.start(); g.get()
16

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

greenlight-0.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distributions

greenlight-0.1-py2.7.egg (6.9 kB view details)

Uploaded Source

greenlight-0.1-py2.6.egg (7.0 kB view details)

Uploaded Source

File details

Details for the file greenlight-0.1.tar.gz.

File metadata

  • Download URL: greenlight-0.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for greenlight-0.1.tar.gz
Algorithm Hash digest
SHA256 515ca5ef2ed221307ecc7081774a1efe3874649e2b3768d284323880060ee7ba
MD5 e0c019b208d0d8a4061adda2adb59840
BLAKE2b-256 8474bd83a731ea7e4574e1ce2aa343026771e4779219970bb1efa9b4111349f8

See more details on using hashes here.

File details

Details for the file greenlight-0.1-py2.7.egg.

File metadata

  • Download URL: greenlight-0.1-py2.7.egg
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for greenlight-0.1-py2.7.egg
Algorithm Hash digest
SHA256 2586686fd38d444ab3d27d850506da6a65347d6a2678b43464f7aad97c90bae1
MD5 e0e419123fe32ee6b008f7bb5d2b4301
BLAKE2b-256 f7cff3845388b0e6932d3549844d4de8afa1de2e1fea82ae239a40bff0eb6a00

See more details on using hashes here.

File details

Details for the file greenlight-0.1-py2.6.egg.

File metadata

  • Download URL: greenlight-0.1-py2.6.egg
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for greenlight-0.1-py2.6.egg
Algorithm Hash digest
SHA256 ca98f75668ac7c0b822eecf6139ac805931f891a53add904a9081081c9cf4ea7
MD5 637190c78b5f8db951005c69d9c28ccf
BLAKE2b-256 951f40c67b1546bc92ac536dab5e3ed45c4fb90e8b0da939e2840bdad30419cb

See more details on using hashes here.

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