Skip to content

Pringley/corolet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

corolet

Use greenlets as coroutines in asyncio.

A corolet is a coroutine compatible with asyncio.coroutines. However, instead of using the yield from keyword to delegate to another coroutine, corolets use a function (corolet.yield_from), allowing subfunctions of a coroutine to delegate.

The idea for corolet was inspired by greenio.

Usage

Create corolets using corolet.corolet. Instead of the yield from keyword, use corolet.yield_from to get a result from an asyncio.Future.

import asyncio
import corolet

@corolet.corolet
def my_corolet():
    print('in a corolet')

    # Corolets are particularly useful when calling subfunctions.
    return subfunction()

def subfunction():
    print('in a subfunction')

    # Non-corolet subfunctions can still call corolet.yield_from to delegate to
    # another coroutine (or corolet).
    result = corolet.yield_from(subcoro())

    return result

@asyncio.coroutine
def subcoro():
    return 3

@asyncio.coroutine
def main():
    # Corolets are still coroutines and can be called from normal asyncio code.
    result = yield from my_corolet()

    print(result) # => 3

# Corolets run in the normal event loop.
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

About

Use greenlets as coroutines in asyncio

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages