Giter VIP home page Giter VIP logo

corocc's Introduction

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.

corocc's People

Contributors

hniksic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.