当前位置:首页 > 日记本 > 正文内容

Python协程简单实例及爬虫实例

zhangchap2年前 (2022-03-26)日记本131
# -*- coding:utf-8 -*-
import asyncio,time

async def func1():
    print('你好啊,我叫赛利亚')
    await asyncio.sleep(2)
    print('你好啊,我叫贱贱')

async def func2():
    print('你好啊,我叫建勋')
    await asyncio.sleep(3)
    print('你好啊,我叫二建')


async def func3():
    print('你好啊,我叫二贱勋')
    await asyncio.sleep(1)
    print('你好啊,我叫二贱贱')

async def main():
    # 第一种写法,不是很推荐
    # f1 = func1()
    # await f1 # 一般await挂起操作放在协程对象前面
    # 第二种写法(推荐)
    # asyncio.wait 写法 Python3.8之后官方就不再建议这么写,3.11之后就要去除这种写法支持
    # tasks = [
    #     func1(),
    #     func2(),
    #     func3()
    # ]
    # await asyncio.wait(tasks)

    # asyncio.wait 写法 Python3.8之后官方就不再建议这么写,3.11之后就要去除这种写法支持
    tasks = [
        asyncio.create_task(func1()),
        asyncio.create_task(func2()),
        asyncio.create_task(func3())
    ]

    await asyncio.wait(tasks)




if __name__ == '__main__':
    s = time.time()
    asyncio.run(main())
    e = time.time()
    print(f'耗时{e - s}')


协程爬虫小实例

async def download(url):
    print('开始下载')
    await asyncio.sleep(2) # 网络请求也要是异步操作 非 requests.get()
    print('下载结束')

async def main():
    urls = [
        'https://www.bilibili.com/',
        'https://www.bilibili.com/video/',
        'https://backstage.dahepiao.com/',
    ]
    tasks = []
    for url in urls:
        d = download(url)
        tasks.append(asyncio.create_task(d))
        # Python3.8以后加上 asyncio.create_task()
        # tasks.append(d)
        # asyncio.wait 写法 Python3.8之后官方就不再建议这么写,3.11之后就要去除这种写法支持

    tasks = [asyncio.create_task(download(url)) for url in urls] # 这样写更简洁
    await asyncio.wait(tasks)


if __name__ == '__main__':
    s = time.time()
    asyncio.run(main())
    e = time.time()
    print(e-s)


标签: Python
分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。