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

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

问题描述

PEP 0492 添加异步关键字到Python 3.5。

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 ...')

根据这个实现了文档

According to the docs this achieves

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

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

这是否异步关键字实际上涉及到的新主题或者利用现有保留的异步线程的创建?

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

在事件异步并使用一个线程保留,它是各自在自己的一个共享线程?

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,它是可以使用的发电机编写协同例程的;通过使用收益前pressions在您创建一个生成对象,而不是函数体,其中$产量当你遍历发生器只执行C $℃。再加上额外的事件循环库(如 ASYNCIO ),你可以写协同例程将信号发送到事件循环,他们将要忙(等待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)

每当上述code前进以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)

由此产生的 协程 的对象仍然需要一个事件循环驱动协同例程;事件循环将等待每个协同例程反过来,这将执行这些协同例程当前不的await ING的东西来完成。

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.

的优点是与原生支持,您还可以引入额外的语法来支持异步上下文管理器和迭代器。进入和退出上下文经理,或在循环迭代器然后可以成为你的合作套路分发出信号,其他code可以运行,而不是因为事情是再次等待。

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天全站免登陆