pythonのgevent同步非同步區別

來源:互聯網
上載者:User

標籤:process   pen   dmi   monk   tps   yahoo   user   from   char   

 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/爬蟲.py4882247981264096同步花費時間: 4.9252817630767824882264096477025非同步花費時間: 3.1961827278137207Process finished with exit code 0

 

pythonのgevent同步非同步區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.