1. 程式人生 > >協程-gevent

協程-gevent

協程-gevent

協程用gevent實現, 依賴libevent和greenlet
###安裝
1. 安裝libevent
yum install libevent

2. 安裝easy_install
【1】wget -q http://peak.telecommunity.com/dist/ez_setup.py
【2】python ez_setup.py
【3】使用easy_install 查看命令是否可用,如果不可用可以講路徑加入到PATH中

3. 安裝greenlet
【1】yum install python-devel
【2】easy_install greenlet
4. 安裝pip
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9

tar zxf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py build
python setup.py install

5. 安裝gevent
wget http://pypi.python.org/packages/source/g/gevent/gevent-0.13.1.tar.gz#md5=5c1b03d9ce39fee4cfe5ea8befb1d4c4
tar zxf gevent-0.13.1.tar.gz
cd gevent-0.13
python fetch_libevent.py
python setup.py build
python setup.py install

6. 安裝request模塊
pip install requests

7. 安裝dnspython模塊
wget http://www.dnspython.org/kits/1.12.0/dnspython-1.12.0.tar.gz
tar -zxf dnspython-1.12.0.tar.gz
cd dnspython-1.12.0
python setup.py install


###案例1
#!/usr/bin/env python
# _*_coding:utf-8_*_

from gevent import monkey
import os
import gevent
import time
monkey.patch_socket()

class MyGeven:

def __init__(self):
self.data = {}

def get_dig_domain(self, domain):
ret = os.system(‘dig %s |egrep -v "^$|;" >>/tmp/tmpcheck‘ % (domain) )
if ret:
self.data[domain] = ret

def gprobe(self, domain_list):
jobs = []
for domain in domain_list:
jobs.append(gevent.spawn(self.get_dig_domain, domain))
gevent.joinall(jobs)

if __name__ == ‘__main__‘:
start_ts = time.time()
print "start....."
domain_list = [
"www.baidu.com",
"www.qq.com",
"www.163.com",
]
obj = MyGeven()
obj.gprobe(domain_list)
print "請求數:", len(domain_list), "耗時:", time.time() - start_ts, "s"
print obj.data


###案例2:
#!/usr/bin/env python
# _*_coding:utf-8_*_
from gevent import monkey
import gevent
import time
monkey.patch_socket()
import requests

class MyGeven:

def __init__(self):
self.data = {}


def get_url_data(self, url):

ret = requests.get(url)
if ret:
self.data[url] = ret


def gprobe(self, url_list):
jobs = []
for url in url_list:
jobs.append(gevent.spawn(self.get_url_data, url))
gevent.joinall(jobs)

if __name__ == ‘__main__‘:
start_ts = time.time()
print "start….."
url_list = [
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
"http://www.baidu.com",
"http://www.qq.com",
"http://www.163.com",
]
obj = MyGeven()
obj.gprobe(url_list)
print "請求數:", len(url_list), "耗時:", time.time() - start_ts, "s"
print obj.data

本文出自 “開發與運維” 博客,謝絕轉載!

協程-gevent