並行請求多個域名的IP地址
阿新 • • 發佈:2018-04-11
python/gevent
以下示例顯示如何同時運行任務。
import gevent
from gevent import socket
urls = [‘www.baidu.com‘, ‘www.sina.com‘, ‘www.sohu.com‘]
jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls]
gevent.joinall(jobs)
[job.value for job in jobs]
[‘112.80.248.73‘, ‘101.71.100.123‘, ‘140.207.205.39‘]
調用函數
# -*-coding=utf-8 -*- from __future__ import print_function import gevent from gevent import monkey monkey.patch_all() import requests urls = [‘http://www.baidu.com‘,‘http://www.sina.com.cn‘,‘http://www.sohu.com‘,‘http://www.baidu.com‘,‘http://www.sina.com.cn‘,‘http://www.sohu.com‘,‘http://www.baidu.com‘,‘http://www.sina.com.cn‘,‘http://www.sohu.com‘] #多些URL可以更好的看到並行執行任務 def print_head(url): print (‘starting %s‘%url) data = requests.get(url).text print (‘%s:%s bytes %r‘%(url,len(data),data[:50])) jobs = [gevent.spawn(print_head,url) for url in urls] gevent.wait(jobs)
並行請求多個域名的IP地址