CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day13/asyncio2.py
Views: 729
1
"""
2
异步I/O操作 - async和await
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-21
7
"""
8
import asyncio
9
import threading
10
11
12
# 通过async修饰的函数不再是普通函数而是一个协程
13
# 注意async和await将在Python 3.7中作为关键字出现
14
async def hello():
15
print('%s: hello, world!' % threading.current_thread())
16
await asyncio.sleep(2)
17
print('%s: goodbye, world!' % threading.current_thread())
18
19
20
loop = asyncio.get_event_loop()
21
tasks = [hello(), hello()]
22
# 等待两个异步I/O操作执行结束
23
loop.run_until_complete(asyncio.wait(tasks))
24
loop.close()
25
26