Skip to content

Latest commit

 

History

History
82 lines (55 loc) · 2.09 KB

README.md

File metadata and controls

82 lines (55 loc) · 2.09 KB

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