# 异步 IO

当代码需要执行一个耗时的 IO 操作时,它只发出 IO 指令,并不等待 IO 结果,然后就去执行其他代码了。一段时间后,当 IO 返回结果时,再通知 CPU 进行处理。

异步 IO 模型需要一个消息循环,在消息循环中,主线程不断地重复 "读取消息 - 处理消息" 这一过程:

1
2
3
4
5
loop = get_event_loop()

while True:
event = loop.get_event()
process_event(event)

消息模型其实早在应用在桌面应用程序中了。一个 GUI 程序的主线程就负责不停地读取消息并处理消息。所有的键盘、鼠标等消息都被发送到 GUI 程序的消息队列中,然后由 GUI 程序的主线程处理。

# async

async/await: two new Python keywords that are used to define coroutines

asyncio: the Python package that provides a foundation and API for running and managing coroutines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import asyncio

@asyncio.coroutine
def hello():
print("hello world")
r = yield from asyncio.sleep(1)
print("hello world again")

# python3
async def hello2():
print("hello python")
r = await asyncio.sleep(1)
print("hello python again")

loop = asyncio.get_event_loop()
tasks = [hello(), hello2()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

# hello python
# hello world
# hello python again
# hello again
Edited on