
from multiprocessing import RLock
import queue


class ConnectorPool():

    def __init__(self,init_num,max_num,method_init,**kwargs):
        self.connector_pool = queue.Queue()
        for i in range(init_num):
            self.connector_pool.put(method_init(**kwargs))
        self.method_init = method_init
        self.kwargs = kwargs
        self._lock = RLock()
        self.pool_size = init_num
        self.max_num = max_num

    def getConnector(self):
        with self._lock:
            if self.connector_pool.empty():
                if self.pool_size<self.max_num:
                    while 1:
                        try:
                            _conn = self.method_init(**self.kwargs)
                            self.connector_pool.put(_conn)
                            break
                        except Exception as e:
                            pass
            _conn = self.connector_pool.get(block=True)
            return _conn

    def putConnector(self,_conn):
        self.connector_pool.put(_conn)

    def decrease(self):
        with self._lock:
            self.pool_size -= 1

    def destory(self):
        while 1:
            try:
                conn = self.connector_pool.get(False)
                conn.close()
            except Exception as e:
                break

    def __del__(self):
        self.destory()




if __name__ == '__main__':
    print(1)
