python爬虫之协程

发布时间:2024年01月22日

1、同步代码:

import time

def 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 asyncio


async def run(i):
    print("lucky is a good man", i)
    # 模拟一个耗时IO
    await 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 asyncio


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


if __name__ == '__main__':
    con = run()
    asyncio.run(con)

运行结果:

run函数开始
run函数结束

文章来源:https://blog.csdn.net/jolinoy/article/details/135745221
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。