计划移植一个无阻塞python的服务框架.
首先要解决http client的使用.
Gcurl已经多年无人维护.
geventhttpclient这个号称比urllib2+gevent monkey快一倍.
先简单试试.
#!/bin/env python
# -*- coding: gbk -*-
#使用pool
import gevent
import gevent.pool
from geventhttpclient import HTTPClient
from geventhttpclient.url import URL
def robot(urls):
#url = URL('http://www.baidu.com/')
url=URL(urls)
http = HTTPClient(url.host)
# issue a get request
response = http.get(url.request_uri)
# read status_code
#response.status_code
print 'status_code(%s) is %s.' % (urls,response.status_code)
# read response body
#body = response.read()
#print 'body is %s.' % body
# close connections
http.close()
def runit(n=2):
#创建节点(协程)
#coros=range(1,n)
pool = gevent.pool.Pool(n)
urls=('http://www.baidu.com','http://www.taobao.com','http://www.sina.com.cn');
for j in xrange(1,n):
#coros[j-1] = gevent.spawn(robot, urls[j-1])
pool.spawn(robot, urls[j-1])
#gevent.joinall(coros)
pool.join()
if __name__ == "__main__":
runit(n=4)