Skip to content

Commit a54ad4c

Browse files
committed
异步IO
1 parent cad1420 commit a54ad4c

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

.idea/workspace.xml

Lines changed: 36 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python基础代码/异步IO学习.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ async def parser(url):
2525
# 给一个函数添加了async关键字,就会把它变成一个异步函数
2626
# 每个线程有一个事件循环,主线程调用asyncio.get_event_loop时会创建事件循环
2727
# 把异步的任务丢给这个循环的run_until_complete方法,事件循环会安排协同程序的执行
28+
# async关键字将一个函数声明为协程函数,函数执行时返回一个协程对象。
29+
# await关键字将暂停协程函数的执行,等待异步IO返回结果。
2830

2931
# start = time.time()
3032
loop = asyncio.get_event_loop()

Python爬虫日记系列/Python爬虫日记九:豌豆荚设计奖三种爬取方法速度对比.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ def method_2(url):
127127

128128
def method_3():
129129
async def get_url(url):
130-
async with aiohttp.ClientSession() as session:
130+
async with aiohttp.ClientSession() as session: # async关键字将一个函数声明为协程函数,函数执行时返回一个协程对象。
131131
async with session.get(url) as html:
132-
response = await html.text(encoding="utf-8")
132+
response = await html.text(encoding="utf-8") # await关键字将暂停协程函数的执行,等待异步IO返回结果。
133133
return response
134134

135135
async def parser(url):

0 commit comments

Comments
 (0)