欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python基于gevent實(shí)現(xiàn)并發(fā)下載器代碼實(shí)例

 更新時(shí)間:2019年11月01日 15:49:13   作者:張風(fēng)閑  
這篇文章主要介紹了python基于gevent實(shí)現(xiàn)并發(fā)下載器代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了python基于gevent實(shí)現(xiàn)并發(fā)下載器代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

并發(fā)下載原理

import gevent
from gevent import monkey
import urllib.request
monkey.patch_all()
def my_download(url):
    print('GET: %s' % url)
    resp = urllib.request.urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))
gevent.joinall([
    gevent.spawn(my_download, "https://www.baidu.com"),
    gevent.spawn(my_download, "https://www.cnblogs.com"),
    gevent.spawn(my_download, "https://www.huya.com")
  
])

運(yùn)行結(jié)果:

GET: https://www.baidu.com
GET: https://www.cnblogs.com
GET: https://www.huya.com
227 bytes received from https://www.baidu.com.
46411 bytes received from https://www.cnblogs.com.
353563 bytes received from https://www.huya.com.

實(shí)現(xiàn)多張圖片同時(shí)下載

import gevent
from gevent import monkey
import urllib.request


monkey.patch_all()


def my_download(url, image_path):
  print('GET: %s' % url)
  resp = urllib.request.urlopen(url)
  data = resp.read()
  print('%d bytes received from %s.' % (len(data), url))
  
  with open(image_path, "wb") as f:
    f.write(data)
gevent.joinall([
  gevent.spawn(my_download, "https://huyaimg.msstatic.com/cdnimage/anchorpost/1099/4c/73ff3e6ce165fb658b2082d4d126c6_2168_1544414129.jpg", "1.jpg"),
  gevent.spawn(my_download, "https://huyaimg.msstatic.com/cdnimage/anchorpost/1020/03/7949816c55f45b9adc9f03d9330af3_2168_1542941499.jpg", "2.jpg"), 
  gevent.spawn(my_download, "https://huyaimg.msstatic.com/cdnimage/anchorpost/1061/55/8071537fa10120c43c5a1359a88530_2168_1545302709.jpg", "3.jpg"),
])

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論