ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

python爬虫之协程

2026/9/30 18:49:38 拓冰建站 浏览量
python爬虫之协程

1、同步代码:

import timedef run(index):print("lucky is a good man", index)time.sleep(2)print("lucky is a nice man", index)for i in range(1, 5):run(i)

运行结果:

lucky is a good man 1
lucky is a nice man 1
lucky is a good man 2
lucky is a nice man 2
lucky is a good man 3
lucky is a nice man 3
lucky is a good man 4
lucky is a nice man 4

2、异步执行

import time
import asyncioasync def run(i):print("lucky is a good man", i)# 模拟一个耗时IOawait asyncio.sleep(2)print("lucky is a nice man", i)if __name__ == "__main__":# loop = asyncio.get_event_loop()  此方法已弃用,使用会报错loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)tasks = []t1 = time.time()for url in range(1, 5):coroutine = run(url)task = asyncio.ensure_future(coroutine)tasks.append(task)loop.run_until_complete(asyncio.wait(tasks))t2 = time.time()print("总耗时:%.2f" % (t2 - t1))

运行结果:

lucky is a good man 1
lucky is a good man 2
lucky is a good man 3
lucky is a good man 4
lucky is a nice man 1
lucky is a nice man 3
lucky is a nice man 2
lucky is a nice man 4
总耗时:2.01

3、简单异步:

import asyncioasync def run():print('run函数开始')#  这个位置的asyncio.sleep()  用于携程对象中的阻塞等待await asyncio.sleep(2)print('run函数结束')if __name__ == '__main__':con = run()asyncio.run(con)

运行结果:

run函数开始
run函数结束