国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python 網(wǎng)絡(luò)爬蟲程序 多任務(wù)異步協(xié)同模板
import asyncio #導(dǎo)入?yún)f(xié)同函數(shù)庫import timeasync def fun1(): print('func1 start') await asyncio.sleep(10) print('func1 start')async def fun2(): print('func2 start') await asyncio.sleep(3) print('func2 start')async def fun3(): print('func3 start') await asyncio.sleep(4) print('func3 start')async def main():#創(chuàng)建協(xié)同任務(wù) tasks=[ asyncio.create_task(fun1()), asyncio.create_task(fun2()), asyncio.create_task(fun3()) ] await asyncio.wait(tasks)if __name__ == '__main__': t1=time.time() asyncio.run(main()) t2=time.time() print(t2-t1)

運行結(jié)果

#網(wǎng)絡(luò)爬蟲異步協(xié)同程序模板import asyncio #導(dǎo)入?yún)f(xié)同函數(shù)庫import timeasync def download(url):    print('正在下載')    await asyncio.sleep(1)    print('下載完成')async def main():    urls = [        'www.baidu.com',        'www.qq.com',        'www.126.com'    ]    task = []    for url in urls:        d = asyncio.create_task(download(url))        task.append(d)    await asyncio.wait(task)if __name__ == '__main__':    t1 = time.time()    asyncio.run(main())    t2 = time.time()    print(t2 - t1)

運行結(jié)果(單線程需要3秒多)

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
《源碼探秘 CPython》90. 剖析 Python 的協(xié)程
python協(xié)程系列(五)——asyncio的核心概念與基本架構(gòu)
python 異步 async/await -1.一文理解什么是協(xié)程
Python編程學(xué)習:這會是你見過講得最清楚的「異步爬蟲指南」
小白學(xué) Python 爬蟲(32):異步請求庫 AIOHTTP 基礎(chǔ)入門
強大的異步爬蟲 with aiohttp
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服