转自:https://zhuanlan.zhihu.com/p/27258289

本文将会讲述Python 3.5之后出现的async/await的使用方法,以及它们的一些使用目的,如果错误,欢迎指正。

昨天看到David Beazley在16年的一个演讲:Fear and Awaiting in Async,给了我不少的感悟和启发,于是想梳理下自己的思路,所以有了以下这篇文章。

Python在3.5版本中引入了关于协程的语法糖async和await,关于协程的概念可以先看我在上一篇文章提到的内容。

看下Python中常见的几种函数形式:

1. 普通函数

deffunction():

return 1

2. 生成器函数

defgenerator():

yield 1

在3.5过后,我们可以使用async修饰将普通函数和生成器函数包装成异步函数和异步生成器。

3. 异步函数(协程)

async defasync_function():

return 1

4. 异步生成器

async defasync_generator():

yield 1

通过类型判断可以验证函数的类型

import types

print(type(function) is types.FunctionType)

print(type(generator()) is types.GeneratorType)

print(type(async_function()) is types.CoroutineType)

print(type(async_generator()) is types.AsyncGeneratorType)

直接调用异步函数不会返回结果,而是返回一个coroutine对象:

print(async_function())

#

协程需要通过其他方式来驱动,因此可以使用这个协程对象的send方法给协程发送一个值:

print(async_function().send(None))

不幸的是,如果通过上面的调用会抛出一个异常:

StopIteration: 1

因为生成器/协程在正常返回退出时会抛出一个StopIteration异常,而原来的返回值会存放在StopIteration对象的value属性中,通过以下捕获可以获取协程真正的返回值:

try:

async_function().send(None)

except StopIteration as e:

print(e.value)

# 1

通过上面的方式来新建一个run函数来驱动协程函数:

defrun(coroutine):

try:

coroutine.send(None)

except StopIteration as e:

return e.value

在协程函数中,可以通过await语法来挂起自身的协程,并等待另一个协程完成直到返回结果:

async defasync_function():

return 1

async defawait_coroutine():

result = await async_function()

print(result)

run(await_coroutine())

# 1

要注意的是,await语法只能出现在通过async修饰的函数中,否则会报SyntaxError错误。

而且await后面的对象需要是一个Awaitable,或者实现了相关的协议。

查看Awaitable抽象类的代码,表明了只要一个类实现了__await__方法,那么通过它构造出来的实例就是一个Awaitable:

classAwaitable(metaclass=ABCMeta):

__slots__ = ()

@abstractmethod

def__await__(self):

yield

@classmethod

def__subclasshook__(cls,C):

if cls is Awaitable:

return _check_methods(C, "__await__")

return NotImplemented

而且可以看到,Coroutine类也继承了Awaitable,而且实现了send,throw和close方法。所以await一个调用异步函数返回的协程对象是合法的。

classCoroutine(Awaitable):

__slots__ = ()

@abstractmethod

defsend(self,value):

...

@abstractmethod

defthrow(self,typ,val=None,tb=None):

...

defclose(self):

...

@classmethod

def__subclasshook__(cls,C):

if cls is Coroutine:

return _check_methods(C, '__await__', 'send', 'throw', 'close')

return NotImplemented

接下来是异步生成器,来看一个例子:

假如我要到一家超市去购买土豆,而超市货架上的土豆数量是有限的:

classPotato:

@classmethod

defmake(cls,num,*args,**kws):

potatos = []

for i in range(num):

potatos.append(cls.__new__(cls, *args, **kws))

return potatos

all_potatos = Potato.make(5)

现在我想要买50个土豆,每次从货架上拿走一个土豆放到篮子:

deftake_potatos(num):

count = 0

while True:

if len(all_potatos) == 0:

sleep(.1)

else:

potato = all_potatos.pop()

yield potato

count += 1

if count == num:

break

defbuy_potatos():

bucket = []

for p in take_potatos(50):

bucket.append(p)

对应到代码中,就是迭代一个生成器的模型,显然,当货架上的土豆不够的时候,这时只能够死等,而且在上面例子中等多长时间都不会有结果(因为一切都是同步的),也许可以用多进程和多线程解决,而在现实生活中,更应该像是这样的:

async deftake_potatos(num):

count = 0

while True:

if len(all_potatos) == 0:

await ask_for_potato()

potato = all_potatos.pop()

yield potato

count += 1

if count == num:

break

当货架上的土豆没有了之后,我可以询问超市请求需要更多的土豆,这时候需要等待一段时间直到生产者完成生产的过程:

async defask_for_potato():

await asyncio.sleep(random.random())

all_potatos.extend(Potato.make(random.randint(1, 10)))

当生产者完成和返回之后,这是便能从await挂起的地方继续往下跑,完成消费的过程。而这整一个过程,就是一个异步生成器迭代的流程:

async defbuy_potatos():

bucket = []

async for p in take_potatos(50):

bucket.append(p)

print(f'Got potato {id(p)}...')

async for语法表示我们要后面迭代的是一个异步生成器。

defmain():

import asyncio

loop = asyncio.get_event_loop()

res = loop.run_until_complete(buy_potatos())

loop.close()

用asyncio运行这段代码,结果是这样的:

Got potato 4338641384...

Got potato 4338641160...

Got potato 4338614736...

Got potato 4338614680...

Got potato 4338614568...

Got potato 4344861864...

Got potato 4344843456...

Got potato 4344843400...

Got potato 4338641384...

Got potato 4338641160...

...

既然是异步的,在请求之后不一定要死等,而是可以做其他事情。比如除了土豆,我还想买番茄,这时只需要在事件循环中再添加一个过程:

defmain():

import asyncio

loop = asyncio.get_event_loop()

res = loop.run_until_complete(asyncio.wait([buy_potatos(), buy_tomatos()]))

loop.close()

再来运行这段代码:

Got potato 4423119312...

Got tomato 4423119368...

Got potato 4429291024...

Got potato 4421640768...

Got tomato 4429331704...

Got tomato 4429331760...

Got tomato 4423119368...

Got potato 4429331760...

Got potato 4429331704...

Got potato 4429346688...

Got potato 4429346072...

Got tomato 4429347360...

...

看下AsyncGenerator的定义,它需要实现__aiter__和__anext__两个核心方法,以及asend,athrow,aclose方法。

classAsyncGenerator(AsyncIterator):

__slots__ = ()

async def__anext__(self):

...

@abstractmethod

async defasend(self,value):

...

@abstractmethod

async defathrow(self,typ,val=None,tb=None):

...

async defaclose(self):

...

@classmethod

def__subclasshook__(cls,C):

if cls is AsyncGenerator:

return _check_methods(C, '__aiter__', '__anext__',

'asend', 'athrow', 'aclose')

return NotImplemented

异步生成器是在3.6之后才有的特性,同样的还有异步推导表达式,因此在上面的例子中,也可以写成这样:

bucket = [p async for p in take_potatos(50)]

类似的,还有await表达式:

result = [await fun() for fun in funcs if await condition()]

除了函数之外,类实例的普通方法也能用async语法修饰:

classThreeTwoOne:

async defbegin(self):

print(3)

await asyncio.sleep(1)

print(2)

await asyncio.sleep(1)

print(1)

await asyncio.sleep(1)

return

async defgame():

t = ThreeTwoOne()

await t.begin()

print('start')

实例方法的调用同样是返回一个coroutine:

function = ThreeTwoOne.begin

method = function.__get__(ThreeTwoOne, ThreeTwoOne())

import inspect

assert inspect.isfunction(function)

assert inspect.ismethod(method)

assert inspect.iscoroutine(method())

同理还有类方法:

classThreeTwoOne:

@classmethod

async defbegin(cls):

print(3)

await asyncio.sleep(1)

print(2)

await asyncio.sleep(1)

print(1)

await asyncio.sleep(1)

return

async defgame():

await ThreeTwoOne.begin()

print('start')

根据PEP 492中,async也可以应用到上下文管理器中,__aenter__和__aexit__需要返回一个Awaitable:

classGameContext:

async def__aenter__(self):

print('game loading...')

await asyncio.sleep(1)

async def__aexit__(self,exc_type,exc,tb):

print('game exit...')

await asyncio.sleep(1)

async defgame():

async with GameContext():

print('game start...')

await asyncio.sleep(2)

在3.7版本,contextlib中会新增一个asynccontextmanager装饰器来包装一个实现异步协议的上下文管理器:

from contextlib import asynccontextmanager

@asynccontextmanager

async defget_connection():

conn = await acquire_db_connection()

try:

yield

finally:

await release_db_connection(conn)

async修饰符也能用在__call__方法上:

classGameContext:

async def__aenter__(self):

self._started = time()

print('game loading...')

await asyncio.sleep(1)

return self

async def__aexit__(self,exc_type,exc,tb):

print('game exit...')

await asyncio.sleep(1)

async def__call__(self,*args,**kws):

if args[0] == 'time':

return time() - self._started

async defgame():

async with GameContext() as ctx:

print('game start...')

await asyncio.sleep(2)

print('game time: ', await ctx('time'))

await和yield from

Python3.3的yield from语法可以把生成器的操作委托给另一个生成器,生成器的调用方可以直接与子生成器进行通信:

defsub_gen():

yield 1

yield 2

yield 3

defgen():

return (yield from sub_gen())

defmain():

for val in gen():

print(val)

# 1

# 2

# 3

利用这一特性,使用yield from能够编写出类似协程效果的函数调用,在3.5之前,asyncio正是使用@asyncio.coroutine和yield from语法来创建协程:

# https://docs.python.org/3.4/library/asyncio-task.html

import asyncio

@asyncio.coroutine

defcompute(x,y):

print("Compute%s+%s..." % (x, y))

yield from asyncio.sleep(1.0)

return x + y

@asyncio.coroutine

defprint_sum(x,y):

result = yield from compute(x, y)

print("%s+%s=%s" % (x, y, result))

loop = asyncio.get_event_loop()

loop.run_until_complete(print_sum(1, 2))

loop.close()

然而,用yield from容易在表示协程和生成器中混淆,没有良好的语义性,所以在Python 3.5推出了更新的async/await表达式来作为协程的语法。

因此类似以下的调用是等价的:

async with lock:

...

with (yield from lock):

...

######################

defmain():

return (yield from coro())

defmain():

return (await coro())

那么,怎么把生成器包装为一个协程对象呢?这时候可以用到types包中的coroutine装饰器(如果使用asyncio做驱动的话,那么也可以使用asyncio的coroutine装饰器),@types.coroutine装饰器会将一个生成器函数包装为协程对象:

import asyncio

import types

@types.coroutine

defcompute(x,y):

print("Compute%s+%s..." % (x, y))

yield from asyncio.sleep(1.0)

return x + y

async defprint_sum(x,y):

result = await compute(x, y)

print("%s+%s=%s" % (x, y, result))

loop = asyncio.get_event_loop()

loop.run_until_complete(print_sum(1, 2))

loop.close()

尽管两个函数分别使用了新旧语法,但他们都是协程对象,也分别称作native coroutine以及generator-based coroutine,因此不用担心语法问题。

下面观察一个asyncio中Future的例子:

import asyncio

future = asyncio.Future()

async defcoro1():

await asyncio.sleep(1)

future.set_result('data')

async defcoro2():

print(await future)

loop = asyncio.get_event_loop()

loop.run_until_complete(asyncio.wait([

coro1(),

coro2()

]))

loop.close()

两个协程在在事件循环中,协程coro1在执行第一句后挂起自身切到asyncio.sleep,而协程coro2一直等待future的结果,让出事件循环,计时器结束后coro1执行了第二句设置了future的值,被挂起的coro2恢复执行,打印出future的结果'data'。

future可以被await证明了future对象是一个Awaitable,进入Future类的源码可以看到有一段代码显示了future实现了__await__协议:

classFuture:

...

def__iter__(self):

if not self.done():

self._asyncio_future_blocking = True

yield self # This tells Task to wait for completion.

assert self.done(), "yield from wasn't used with future"

return self.result() # May raise too.

if compat.PY35:

__await__ = __iter__ # make compatible with 'await' expression

当执行await future这行代码时,future中的这段代码就会被执行,首先future检查它自身是否已经完成,如果没有完成,挂起自身,告知当前的Task(任务)等待future完成。

当future执行set_result方法时,会触发以下的代码,设置结果,标记future已经完成:

defset_result(self,result):

...

if self._state != _PENDING:

raise InvalidStateError('{}: {!r}'.format(self._state, self))

self._result = result

self._state = _FINISHED

self._schedule_callbacks()

最后future会调度自身的回调函数,触发Task._step()告知Task驱动future从之前挂起的点恢复执行,不难看出,future会执行下面的代码:

classFuture:

...

def__iter__(self):

...

assert self.done(), "yield from wasn't used with future"

return self.result() # May raise too.

最终返回结果给调用方。

前面讲了那么多关于asyncio的例子,那么除了asyncio,就没有其他协程库了吗?asyncio作为python的标准库,自然受到很多青睐,但它有时候还是显得太重量了,尤其是提供了许多复杂的轮子和协议,不便于使用。

你可以理解为,asyncio是使用async/await语法开发的协程库,而不是有asyncio才能用async/await,除了asyncio之外,curio和trio是更加轻量级的替代物,而且也更容易使用。

curio的作者是David Beazley,下面是使用curio创建tcp server的例子,据说这是dabeaz理想中的一个异步服务器的样子:

from curio import run, spawn

from curio.socket import *

async defecho_server(address):

sock = socket(AF_INET, SOCK_STREAM)

sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

sock.bind(address)

sock.listen(5)

print('Server listening at', address)

async with sock:

while True:

client, addr = await sock.accept()

await spawn(echo_client, client, addr)

async defecho_client(client,addr):

print('Connection from', addr)

async with client:

while True:

data = await client.recv(100000)

if not data:

break

await client.sendall(data)

print('Connection closed')

if __name__ == '__main__':

run(echo_server, ('',25000))

无论是asyncio还是curio,或者是其他异步协程库,在背后往往都会借助于IO的事件循环来实现异步,下面用几十行代码来展示一个简陋的基于事件驱动的echo服务器:

from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR

from selectors import DefaultSelector, EVENT_READ

selector = DefaultSelector()

pool = {}

defrequest(client_socket,addr):

client_socket, addr = client_socket, addr

defhandle_request(key,mask):

data = client_socket.recv(100000)

if not data:

client_socket.close()

selector.unregister(client_socket)

del pool[addr]

else:

client_socket.sendall(data)

return handle_request

defrecv_client(key,mask):

sock = key.fileobj

client_socket, addr = sock.accept()

req = request(client_socket, addr)

pool[addr] = req

selector.register(client_socket, EVENT_READ, req)

defecho_server(address):

sock = socket(AF_INET, SOCK_STREAM)

sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

sock.bind(address)

sock.listen(5)

selector.register(sock, EVENT_READ, recv_client)

try:

while True:

events = selector.select()

for key, mask in events:

callback = key.data

callback(key, mask)

except KeyboardInterrupt:

sock.close()

if __name__ == '__main__':

echo_server(('',25000))

验证一下:

# terminal 1

$ nc localhost 25000

hello world

hello world

# terminal 2

$ nc localhost 25000

hello world

hello world

现在知道,完成异步的代码不一定要用async/await,使用了async/await的代码也不一定能做到异步,async/await是协程的语法糖,使协程之间的调用变得更加清晰,使用async修饰的函数调用时会返回一个协程对象,await只能放在async修饰的函数里面使用,await后面必须要跟着一个协程对象或Awaitable,await的目的是等待协程控制流的返回,而实现暂停并挂起函数的操作是yield。

个人认为,async/await以及协程是Python未来实现异步编程的趋势,我们将会在更多的地方看到他们的身影,例如协程库的curio和trio,web框架的sanic,数据库驱动的asyncpg等等...在Python 3主导的今天,作为开发者,应该及时拥抱和适应新的变化,而基于async/await的协程凭借良好的可读性和易用性日渐登上舞台,看到这里,你还不赶紧上车?

python2异步编程_python异步编程 (转载)相关推荐

  1. python socket编程_Python Socket编程实现网络编程

    对于有经验的开发人员来说,掌握的编程语言应该是不少的.在这些编程语言中,网络编程的应用时必不可少的.其中Python也是这样的编程语言.我们今天将会在这里为大家详细介绍一下Python Socket编 ...

  2. python2异步编程_python异步编程入门

    这几天看代码,总是会接触到很多异步编程,之前只想着实现功能,从来没考虑过代码的运行快慢问题,故学习一番. 从0到1,了解python异步编程的演进 1.urllib与requests爬虫 reques ...

  3. python2异步编程_Python 异步编程入门

    本文是写给 JavaScript 程序员的 Python 教程. Python 的异步编程,其他人可能觉得很难,但是 JavaScript 程序员应该特别容易理解,因为两者的概念和语法类似.JavaS ...

  4. python的异步网络编程_python异步网络编程怎么使socket关闭之后立即执行一段代码?...

    import socket from _socket import getdefaulttimeout class MySocket(socket.socket): """ ...

  5. python执行cmd命令行异步执行_Python 异步调用命令行工具

    当你在自己的 Python 程序中采用了基于事件循环的异步编程方法之后,你就会发现自己不自觉地被其牢牢吸引住,并不是说这一方法多么棒,而是因为你不得不想办法保证程序中的任意环节都不能是阻塞的! 例如当 ...

  6. python事件驱动编程_Python事件驱动编程

    事件驱动的编程侧重于事件.最终,程序的流程取决于事件.到目前为止,我们处理顺序或并行执行模型,但具有事件驱动编程概念的模型称为异步模型.事件驱动的编程取决于始终侦听新传入事件的事件循环.事件驱动编程的 ...

  7. python高并发编程_python 并发编程

    一 背景知识 一 操作系统的发展 没有操作系统----穿孔卡片(对应程序和数据) 特点:手工慢与计算机高速形成极大矛盾. (1)用户独占全机. (2)CPU等待手工操作. 批处理系统---磁带存储 控 ...

  8. python 窗体编程_python窗体编程

    广告关闭 云服务器1核2G首年99年,还有多款热门云产品满足您的上云需求 python里的图形化界面(gui)模块主要有tkinter(python自带).pyqt.wxpython,我们这节主要讲解 ...

  9. python高并发编程_Python——并发编程

    开始说并发编程之前,最好有一定的底层知识积累,这里我把需要的知识总结了一下,如果看下面的有不理解的可以看一下:https://www.cnblogs.com/kuxingseng95/p/941820 ...

最新文章

  1. linux 安装jdk yum安装 源码包安装
  2. 百度相关搜索软件_Python与seo,百度关键词相关搜索关键词采集源码
  3. 跨时钟域电路设计——单bit信号
  4. c#:细说时区、DateTime和DateTimeOffset在国际化中的应用
  5. r psm倾向性匹配_南瓜香料指标psm如何规划季节性广告
  6. 前端学习(581):使用element调试dom 查看和调试dom节点
  7. 新网域名查询和注册API接口类 源码
  8. 4复数与复变函数(四)
  9. 解决Scrapy抓取中文网页保存为json文件时中文不显示而是显示unicode的问题
  10. Spring使用XML的方式实现AOP的开发——Spring AOP(六)
  11. jenkins+donet core持续集成环境搭建
  12. 解决Ubuntu18.04和Win10双系统系统时间不对的问题
  13. 信息学奥赛一本通 1197 山区建小学(区间DP)
  14. 9V降压5V低功耗恒压稳压芯片,大电流3A方案和LDO
  15. java 微信给好友发信息吗,不在线,好友发给我的微信消息,会不会丢?
  16. 华为模拟器dhcp中继
  17. Vue项目中Echarts流向图迁徙图实现
  18. TYPORA的使用手册
  19. 三极管PNP与NPN控制区别
  20. Pycharm配置——解释器(interpreter)

热门文章

  1. Ubuntu16.04能识别U盘,但无法识别光盘
  2. 使用Apache Archiva管理Maven仓库
  3. MySQL bin-log 日志清理方式
  4. LeetCode Keyboard Row
  5. ArcGIS API for Silverlight 实现修改地图上的工程点位置
  6. WINCE的内存配置
  7. Linux系统查看分区文件系统类型
  8. 关于Raid0,Raid1,Raid5,Raid10的总结
  9. 男人一辈子就喜欢一种类型的女人,至死不渝从一而终!
  10. les物流执行系统_物流规划工作如何开展?