Giter Site home page Giter Site logo

py-coroutine's Introduction

py-coro-impl

A stackless coroutine implementation in Python.

Contains coroutine-level timer, and a simple non-blocking socket implementation of the coroutine-level HTTP protocol interface.

run example

python3 test.py
import time
import math
from asynclib.core import Promise, LoopManager, Timer
from asynclib.asynchttp import get as asyncget


@LoopManager.asyncfun
def httpReq():
    responseData = yield from Promise(
        lambda resolve:
            asyncget(
                url='http://codingfor.life/doc/README.md',
                callback=lambda response: resolve(response)
            ),
    ).catch(lambda e: print(e))
    return responseData


@ LoopManager.asyncfun
def asyncmain():
    # async call
    print('async call start', '\n')
    start = time.time()
    print('async res: ', (yield from Promise.all([httpReq() for _ in range(10)])), '\n')
    print('promise all cost: ' +
          str(math.floor((time.time() - start) * 1000)) + 'ms', '\n')

    # sleep a while using Timer
    print('sleep 2000ms using coroutine timer', '\n')
    yield from Timer.sleep(2000)

    # sync call
    print('sync call start', '\n')
    start = time.time()
    for _ in range(10):
        print(f'sync res {_}: ', (yield from httpReq()), '\n')
    print('sync cost: ' +
          str(math.floor((time.time() - start) * 1000)) + 'ms', '\n')


asyncmain()

reference

py-coroutine's People

Contributors

drincann avatar bbq293 avatar

Stargazers

loser-guo avatar LIU SHANGYU avatar Kingking avatar  avatar  avatar

Watchers

 avatar

Forkers

bbq293

py-coroutine's Issues

feature(eventloop): 实现事件循环的自动结束

当所有异步任务结束后,停止事件循环,结束主线程。

可能需要抽象一个实体,对异步任务进行计数。

此外,还应考虑失败的异步任务,例如设置连接超时时间,抛出异常,令 Future 失败,这里还需要修改 Future。

refactor(timer): 在单独的线程中检查计时器

目前的计时器实现在事件循环线程,作为事件队列中的任务,iotime 会严重影响计时器的准确性。
最好能在新线程中唤醒正在阻塞的事件循环,从而准确执行定时器。

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.