100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python爬虫 单线程的多任务异步协程

python爬虫 单线程的多任务异步协程

时间:2023-10-03 04:42:13

相关推荐

python爬虫 单线程的多任务异步协程

在input()、sleep(2)、request.get()等时,都会导致线程阻塞,协程可以解决IO等操作时的阻塞现象,提高CPU利用效率。

1.单线程的多任务异步协程

main.py

"""=== coding: UTF8 ==="""import asyncioimport timeasync def func1():print("hello python1")await asyncio.sleep(2)print("hello python1")async def func2():print("hello python2")await asyncio.sleep(3)print("hello python2")async def func3():print("hello python3")await asyncio.sleep(4)print("hello python3")async def main():# 准备异步协程对象列表tasks = [asyncio.create_task(func1()),asyncio.create_task(func2()),asyncio.create_task(func3())]await asyncio.wait(tasks)"""========================================主函数功能测试========================================"""if __name__ == '__main__':t1 = time.time()# 一次性运行多个任务asyncio.run(main())t2 = time.time()print(t2 - t1) # 打印耗时的时间

运行效果:

"C:\Program Files\Python38\python.exe" E:/PythonSourceCode/test/main.pyhello python1hello python2hello python3hello python1hello python2hello python34.022439956665039Process finished with exit code 0

2.单线程的多任务异步协程在爬虫领域的模拟应用

只是模拟,可以作为模板,在实际爬虫时,需要重点解决

await asyncio.sleep(3) # 模拟网络请求(网络耗时)

main.py

"""=== coding: UTF8 ==="""import asyncioimport time# 在爬虫领域的应用async def download(url):print("准备开始下载")await asyncio.sleep(3) # 模拟网络请求(网络耗时)print("下载完成")async def main():urls = ["/", "/", "/"]# 准备异步协程对象列表tasks = []for url in urls:d = asyncio.create_task(download(url))tasks.append(d)await asyncio.wait(tasks)"""========================================主函数功能测试========================================"""if __name__ == '__main__':t1 = time.time()# 一次性运行多个任务asyncio.run(main())t2 = time.time()print(t2 - t1) # 打印耗时的时间

关注公众号,获取更多资料

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。