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()