Skip to content
forked from Pringley/corolet

Use greenlets as coroutines in asyncio

License

Notifications You must be signed in to change notification settings

rokups/goroutine

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goroutine

Use greenlets as coroutines in asyncio.

A goroutine (a green coroutine) is a coroutine compatible with asyncio.coroutines. However, instead of using the yield from or await keywords to delegate to another coroutine, goroutines use a function goroutine.yield_from, allowing subfunctions of a coroutine to schedule other coroutines.

The idea for goroutine was inspired by greenio.

Usage

Create goroutines using goroutine.goroutine decorator. Instead of the yield from keyword, use goroutine.yield_from to get a result from an asyncio.Future.

import asyncio
from goroutine import goroutine, yield_from


@goroutine
def my_goroutine():
    print('in a goroutine')

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


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

    # Non-goroutine subfunctions can still call yield_from to delegate to
    # another coroutine (or goroutine).
    result = 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_goroutine()

    print(result)     # => 3

if __name__ == '__main__':
    # Goroutines 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

  • Python 100.0%