Skip to content
/ corocc Public

Coroutines with explicit suspend and continuation

License

Notifications You must be signed in to change notification settings

hniksic/corocc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

corocc

The corocc module implements execution of coroutines with explicit suspend and continuation, inspired by Kotlin.

This is useful for integrating coroutines with environments that can't run a full PEP 3156 event loop, but that still provide support for executing callbacks.

See the blog post for details about the topic.

Examples:

corocc allows a coroutine to willingly suspend itself and, before suspension, get a reference to a callable that will continue its execution. Here is a very simple example that just stores the continuation in a global variable:

import corocc

async def always_suspending():
    global cont
    i = 1
    while True:
        async with corocc.suspending() as cont:
            print('suspension', i)
            i += 1

>>> corocc.start(always_suspending())
suspension 1
>>> cont()
suspension 2
>>> cont()
suspension 3
...

This doesn't look too useful since driving a coroutine from the outside can be achieved with the send coroutine method. But corocc allows for the coroutine to continue itself, without relying on an outside agent to "drive" it.

For example, this is an equivalent of asyncio.sleep() which continues the current coroutine in a different thread:

import corocc, threading

async def thread_sleep(delay):
    async with corocc.suspending() as cont:
        t = threading.Timer(delay, cont)
        t.start()

async def greet():
    print('hello...')
    await thread_sleep(1)
    print('...world')

>>> corocc.start(greet())
hello...
>>> ...world

An equivalent coroutine that uses the GLib main loop:

async def glib_sleep(delay):
    async with corocc.suspending() as cont:
        GLib.timeout_add(delay * 1000, cont)

License

corocc is distributed under the terms of the MIT license, see LICENSE-MIT for details. Contributing changes is assumed to signal agreement with these licensing terms.

About

Coroutines with explicit suspend and continuation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages