`
san_yun
  • 浏览: 2597035 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

实现一个连接池

 
阅读更多
在程序中大部分对象都不需要重复利用,每次用完直接丢弃就可以了,但有一种对象不能这样做---TCP connection(socket),socket如果每次用完直接关闭会导致几个问题:
1.netstat 看TIME_WAIT或CLOSE_WAIT非常高。
2.每次重新建立连接需要消耗资源。

通过实现连接池能比较好的解决这个问题,redis,mongo客户端都实现了连接池,通过apache-common提供的object pool也能实现一个比较好的连接池,下面是模仿redis-py实现的一段连接池。




import threading
from time import sleep
import sys

class Connection(object):
	def __init__(self):
		self.conn = "conn"

class ConnectionPool(object):  
	def __init__(self):
		self.max_connections = 10
		self._created_connections = 0  
		self._available_connections = []  
		self._in_use_connections = set()  

	def get_connection(self):
		try: 
			connection = self._available_connections.pop()  
		except IndexError:  
			connection = self.make_connection()
		self._in_use_connections.add(connection)
		return connection  

	def make_connection(self):
		
		if self._created_connections >= self.max_connections:  
			raise Error("Too many connections")  
		self._created_connections += 1  
		return Connection() 

	def release(self, connection):  
		
		self._in_use_connections.remove(connection)  
		self._available_connections.append(connection)
	
class TestThread(threading.Thread):
	def __init__(self,testobj):
		threading.Thread.__init__(self)
		self.pool = testobj

	def run(self):
		while True:
			connect = self.pool.get_connection()
			sleep(1)			
			self.pool.release(connect)
			#print  str(self.pool._created_connections)+":"+str(len(self.pool._in_use_connections))

test = ConnectionPool()

for i in xrange(8):
	t = TestThread(test)
	t.start()  

  • 大小: 30.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics