PEP 0492 - Python 3.5 异步关键字 [英] PEP 0492 - Python 3.5 async keyword

查看:29
本文介绍了PEP 0492 - Python 3.5 异步关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PEP 0492 在 Python 3.5 中添加了 async 关键字.

PEP 0492 adds the async keyword to Python 3.5.

Python 如何从使用此运算符中受益?为协程提供的示例是

How does Python benefit from the use of this operator? The example that is given for a coroutine is

async def read_data(db):
    data = await db.fetch('SELECT ...')

根据文档,这实现了

暂停[ing] read_data协程的执行,直到db.fetch awaitable完成并返回结果数据.

suspend[ing] execution of read_data coroutine until db.fetch awaitable completes and returns the result data.

这个 async 关键字是否实际上涉及创建新线程或使用现有的保留异步线程?

Does this async keyword actually involve creation of new threads or perhaps the use of an existing reserved async thread?

如果 async 确实使用了保留线程,那么它是否是一个单独的共享线程?

In the event that async does use a reserved thread, is it a single shared thread each in their own?

推荐答案

不,协程不涉及任何类型的线程.协同程序允许合作多任务处理,因为每个协同程序都自愿放弃控制.另一方面,线程在任意点在单元之间切换.

No, co-routines do not involve any kind of threads. Co-routines allow for cooperative multi-tasking in that each co-routine yields control voluntarily. Threads on the other hand switch between units at arbitrary points.

在 Python 3.4 之前,可以使用 generators 编写协程;通过在函数体中使用 yieldyield from 表达式,您可以创建一个生成器对象,其中代码仅在您迭代生成器时执行.与其他事件循环库(例如 asyncio)一起,您可以编写 co- 向事件循环发出信号表示它们将忙(可能在等待 I/O)并且同时可以运行另一个协同例程的例程:

Up to Python 3.4, it was possible to write co-routines using generators; by using yield or yield from expressions in a function body you create a generator object instead, where code is only executed when you iterate over the generator. Together with additional event loop libraries (such as asyncio) you could write co-routines that would signal to an event loop that they were going to be busy (waiting for I/O perhaps) and that another co-routine could be run in the meantime:

import asyncio
import datetime

@asyncio.coroutine
def display_date(loop):
    end_time = loop.time() + 5.0
    while True:
        print(datetime.datetime.now())
        if (loop.time() + 1.0) >= end_time:
            break
        yield from asyncio.sleep(1)

每次上述代码前进到 yield from asyncio.sleep(1) 行时,事件循环可以自由地运行不同的协程,因为该例程不会做任何事情下一秒无论如何.

Every time the above code advances to the yield from asyncio.sleep(1) line, the event loop is free to run a different co-routine, because this routine is not going to do anything for the next second anyway.

因为生成器可以用于各种任务,而不仅仅是协同程序,并且因为使用生成器语法编写协同程序可能会让新手感到困惑,所以 PEP 引入了新的语法,使其更清晰 你正在编写一个协程.

Because generators can be used for all sorts of tasks, not just co-routines, and because writing a co-routine using generator syntax can be confusing to new-comers, the PEP introduces new syntax that makes it clearer that you are writing a co-routine.

实施 PEP 后,上述示例可以改写为:

With the PEP implemented, the above sample could be written instead as:

async def display_date(loop):
    end_time = loop.time() + 5.0
    while True:
        print(datetime.datetime.now())
        if (loop.time() + 1.0) >= end_time:
            break
        await asyncio.sleep(1)

生成的 coroutine 对象仍然需要一个事件循环来驱动协同程序;一个事件循环将依次await每个协程,这将执行那些当前没有await完成某事的协程.

The resulting coroutine object still needs an event loop to drive the co-routines; an event loop would await on each co-routine in turn, which would execute those co-routines that are not currently awaiting for something to complete.

优点是有了原生支持,你还可以引入额外的语法来支持异步上下文管理器和迭代器.进入和退出上下文管理器,或循环遍历一个迭代器可能会成为你的协程中更多的点,表明其他代码可以运行,因为有东西再次等待.

The advantages are that with native support, you can also introduce additional syntax to support asynchronous context managers and iterators. Entering and exiting a context manager, or looping over an iterator then can become more points in your co-routine that signal that other code can run instead because something is waiting again.

这篇关于PEP 0492 - Python 3.5 异步关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆