如何在 Python 中编写一系列承诺?

新手上路,请多包涵

是否可以 使用 Python 3.6.1 标准库 编写一系列 承诺(或任务)?

例如,JavaScript 中的一个序列 promise 写成:

 const SLEEP_INTERVAL_IN_MILLISECONDS = 200;

const alpha = function alpha (number) {
    return new Promise(function (resolve, reject) {
        const fulfill = function() {
            return resolve(number + 1);
        };

        return setTimeout(fulfill, SLEEP_INTERVAL_IN_MILLISECONDS);
    });
};

const bravo = function bravo (number) {
    return new Promise(function (resolve, reject) {
        const fulfill = function() {
            return resolve(Math.ceil(1000*Math.random()) + number);
        };
        return setTimeout(fulfill, SLEEP_INTERVAL_IN_MILLISECONDS);
    });
};

const charlie = function charlie (number) {
    return new Promise(function (resolve, reject) {
        return (number%2 == 0) ? reject(number) : resolve(number);
    });
};

function run() {
    return Promise.resolve(42)
        .then(alpha)
        .then(bravo)
        .then(charlie)
        .then((number) => {
            console.log('success: ' + number)
        })
        .catch((error) => {
            console.log('error: ' + error);
        });
}

run();

每个函数 还返回一个具有异步处理结果的 Promise ,该结果将被紧随其后的 Promise 解决/拒绝。

我知道诸如 promises-2.01basyncio 3.4.3 之类的库,我正在寻找 Python STL 解决方案。因此,如果我需要导入一个非 STL 库,我更喜欢使用 RxPython

原文由 JP Ventura 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 642
2 个回答

这是一个使用 asyncio 和 async/await 语法的类似程序:

 import asyncio
import random

async def alpha(x):
    await asyncio.sleep(0.2)
    return x + 1

async def bravo(x):
    await asyncio.sleep(0.2)
    return random.randint(0, 1000) + x

async def charlie(x):
    if x % 2 == 0:
        return x
    raise ValueError(x, 'is odd')

async def run():
    try:
        number = await charlie(await bravo(await alpha(42)))
    except ValueError as exc:
        print('error:', exc.args[0])
    else:
        print('success:', number)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()


编辑:如果您对反应流感兴趣,您可以考虑使用 aiostream

这是一个简单的例子:

 import asyncio
from aiostream import stream, pipe

async def main():
    # This stream computes 11² + 13² in 1.5 second
    xs = (
        stream.count(interval=0.1)      # Count from zero every 0.1 s
        | pipe.skip(10)                 # Skip the first 10 numbers
        | pipe.take(5)                  # Take the following 5
        | pipe.filter(lambda x: x % 2)  # Keep odd numbers
        | pipe.map(lambda x: x ** 2)    # Square the results
        | pipe.accumulate()             # Add the numbers together
    )
    print('11² + 13² = ', await xs)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

文档 中的更多示例。

免责声明:我是项目维护者。

原文由 Vincent 发布,翻译遵循 CC BY-SA 4.0 许可协议

你很幸运,Python 3.4 及更高版本包括 asyncio ,尽管你正在寻找的功能 ( Future ) 在 Python 3.5 及更高版本中可用。

从您自己关于 asyncio 的链接中:“此版本仅与 Python 3.3 相关,它的 stdlib 中不包含 asyncio。”

例子:

 import asyncio

async def some_coroutine():
    await asyncio.sleep(1)
    return 'done'

def process_result(future):
    print('Task returned:', future.result())

loop = asyncio.get_event_loop()
task = loop.create_task(some_coroutine())
task.add_done_callback(process_result)
loop.run_until_complete()

原文由 Wolph 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题