一个类似芹菜的事件循环,异步且不再依赖

asyncloop的Python项目详细描述


一个类似芹菜的事件循环,具有'asyncio',无依赖项

它在单独的线程中运行asyncio事件循环,在循环中驱动本机协程,然后以异步方式返回未来。

依赖性

它需要Python3.5+。

安装

$ git clone https://github.com/dgkim5360/asyncloop.git
$ cd asyncloop
asyncloop$ python setup.py install

开始

import asyncio as aio

from asyncloop import AsyncLoop


# A simple job, which should be a native coroutine
async def job_to_wait(sleep_sec):
    await aio.sleep(sleep_sec)
    return sleep_sec


# A simple callback
def callback(fut):
    if fut.cancelled():
        print('CANCELLED:', fut)
    elif fut.done():
        print('DONE:', fut)
        print('RESULT:', fut.result()


# AsyncLoop starts
aloop = AsyncLoop()  # <AsyncLoop(Thread-##, initial)>
aloop.start()  # <AsyncLoop(Thread-##, started ##########)>

# Submit a job and be free to work on
# it returns an AsyncJob object, a simple wrapper of concurrent.Future
ajob = aloop.submit(job_to_wait(10), callback)
ajob  # <AsyncJob at 0x####>

# After 10 seconds the callback activated
# DONE: <Future at 0x#### state=finished returned int>
# RESULT: 10

# Get a result
ret = ajob.result()  # 10

# You MUST stop the aloop before exit or destroy
aloop.stop()  # <AsyncLoop(Thread-##, stopped ##########)>

到目前为止,仅此而已。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何设置用户的特定输入,即仅8位数字?   java嗅探访问的站点并保存到文件   java系统。出来同时使用字符串和int时的println行为   java在一个变量的右边可以做多少算术运算   java我可以将RequestToken发送到web浏览器,还是应该将其存储在数据库中?   java Android错误:应用程序意外停止,请重试   Java中的字符串如何提取   java向XmlObject的所有标记添加前缀   web服务无法从同一网络访问web服务   静态变量和实例变量之间的java性能差异   java无法从Thymeleaf获取transactionsynchronized会话   java为什么实现runnable总是交织结果?   宽度优先搜索Java最短距离   java Hibernate额外懒惰不工作   java为什么程序稍后会输出代码中的其他内容?   java如何在正则表达式中使用插入符号和美元?   java即时搜索/在JTable单元格中自动完成   这个java forloop是如何工作的?   java NullPointerException MainFragment facebook登录