pythonのgevent同步异步区别
生活随笔
收集整理的這篇文章主要介紹了
pythonのgevent同步异步区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #!/usr/bin/env python
2
3 from urllib import request
4 import gevent
5 from gevent import monkey
6 import time
7
8 monkey.patch_all() # 把當前程序所有的IO操作給我單獨的做上標記。
9 def f(url):
10 resp = request.urlopen(url)
11 data = resp.read()
12 print(len(data))
13
14 urls = ["https://www.python.org",
15 "https://www.yahoo.com",
16 "https://github.com"
17 ]
18 time_start = time.time()
19 for url in urls:
20 f(url)
21 print("同步花費時間:",time.time()-time_start)
22
23 asy_time = time.time()
24 gevent.joinall({
25 gevent.spawn(f,"https://www.python.org"),
26 gevent.spawn(f,"https://www.yahoo.com"),
27 gevent.spawn(f,"https://github.com"),
28 })
29 print("異步花費時間:",time.time()-asy_time) C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/PythonStudy/charm/01/day10/爬蟲.py
48822
479812
64096
同步花費時間: 4.925281763076782
48822
64096
477025
異步花費時間: 3.1961827278137207Process finished with exit code 0
?
轉載于:https://www.cnblogs.com/pengpengzhang/p/9585034.html
總結
以上是生活随笔為你收集整理的pythonのgevent同步异步区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9.3学习笔记
- 下一篇: 华为代码质量军规 (1) 数组访问,必须