Python sqlalchemy.pool.QueuePool() Examples
The following are 25
code examples of sqlalchemy.pool.QueuePool().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy.pool
, or try the search function
.
Example #1
Source File: orm.py From girlfriend with MIT License | 6 votes |
def test_init_all(self): engine_manager = EngineManager() # 同一个manager对象,重复初始化不会发生任何问题 for i in xrange(0, 100): engine_manager.validate_config(self.config) engine_manager.init_all(self.config) test_engine = engine_manager.engine("test") self.assertIsNotNone(test_engine) # 连接池相关参数 pool = test_engine.engine.pool self.assertIsInstance(pool, QueuePool) self.assertEquals(pool.size(), 10) self.assertEquals(pool._recycle, 3600) self.assertEquals(pool._timeout, 20) # 测试可否连接 connection = test_engine.engine.connect() result = connection.execute("select 1 + 1") self.assertEquals(tuple(result)[0][0], 2) test_engine2 = engine_manager.engine("test2") self.assertIsNotNone(test_engine2) connection = test_engine2.engine.connect() result = connection.execute("select * from user") self.assertEquals(tuple(result)[0], (1, "SamChi", 1, "I am SamChi"))
Example #2
Source File: test_pool.py From sqlalchemy with MIT License | 6 votes |
def _assert_cleanup_on_pooled_reconnect(self, dbapi, p): # p is QueuePool with size=1, max_overflow=2, # and one connection in the pool that will need to # reconnect when next used (either due to recycle or invalidate) with self._no_wr_finalize(): eq_(p.checkedout(), 0) eq_(p._overflow, 0) dbapi.shutdown(True) assert_raises_context_ok(Exception, p.connect) eq_(p._overflow, 0) eq_(p.checkedout(), 0) # and not 1 dbapi.shutdown(False) c1 = self._with_teardown(p.connect()) # noqa assert p._pool.empty() # poolsize is one, so we're empty OK c2 = self._with_teardown(p.connect()) # noqa eq_(p._overflow, 1) # and not 2 # this hangs if p._overflow is 2 c3 = self._with_teardown(p.connect()) c3.close()
Example #3
Source File: staticnode.py From syncserver with Mozilla Public License 2.0 | 6 votes |
def __init__(self, sqluri, node_url, **kw): self.sqluri = sqluri self.node_url = node_url self.driver = urlparse(sqluri).scheme.lower() sqlkw = { "logging_name": "syncserver", "connect_args": {}, "poolclass": QueuePool, "pool_reset_on_return": True, } if self.driver == "sqlite": # We must mark it as safe to share sqlite connections between # threads. The pool will ensure there's no race conditions. sqlkw["connect_args"]["check_same_thread"] = False # If using a :memory: database, we must use a QueuePool of # size 1 so that a single connection is shared by all threads. if urlparse(sqluri).path.lower() in ("/", "/:memory:"): sqlkw["pool_size"] = 1 sqlkw["max_overflow"] = 0 if "mysql" in self.driver: # Guard against the db closing idle conections. sqlkw["pool_recycle"] = kw.get("pool_recycle", 3600) self._engine = create_engine(sqluri, **sqlkw) users.create(self._engine, checkfirst=True)
Example #4
Source File: test_reconnect.py From sqlalchemy with MIT License | 6 votes |
def _pool_fixture(self, pre_ping, pool_kw=None): dialect = url.make_url( "postgresql://foo:bar@localhost/test" ).get_dialect()() dialect.dbapi = self.dbapi _pool = pool.QueuePool( creator=lambda: self.dbapi.connect("foo.db"), pre_ping=pre_ping, dialect=dialect, **(pool_kw if pool_kw else {}) ) dialect.is_disconnect = lambda e, conn, cursor: isinstance( e, MockDisconnect ) return _pool
Example #5
Source File: test_pool.py From sqlalchemy with MIT License | 6 votes |
def test_no_connect_on_recreate(self): def creator(): raise Exception("no creates allowed") for cls in ( pool.SingletonThreadPool, pool.StaticPool, pool.QueuePool, pool.NullPool, pool.AssertionPool, ): p = cls(creator=creator) p.dispose() p2 = p.recreate() assert p2.__class__ is cls mock_dbapi = MockDBAPI() p = cls(creator=mock_dbapi.connect) conn = p.connect() conn.close() mock_dbapi.connect.side_effect = Exception("error!") p.dispose() p.recreate()
Example #6
Source File: test_pool.py From sqlalchemy with MIT License | 6 votes |
def test_overflow_reset_on_failed_connect(self): dbapi = Mock() def failing_dbapi(): time.sleep(2) raise Exception("connection failed") creator = dbapi.connect def create(): return creator() p = pool.QueuePool(creator=create, pool_size=2, max_overflow=3) c1 = self._with_teardown(p.connect()) # noqa c2 = self._with_teardown(p.connect()) # noqa c3 = self._with_teardown(p.connect()) # noqa eq_(p._overflow, 1) creator = failing_dbapi assert_raises(Exception, p.connect) eq_(p._overflow, 1)
Example #7
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def test_queue_pool(self): self._do_test(pool.QueuePool, ["R", "CL", "R"])
Example #8
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def test_lifo(self): c1, c2, c3 = Mock(), Mock(), Mock() connections = [c1, c2, c3] def creator(): return connections.pop(0) p = pool.QueuePool(creator, use_lifo=True) pc1 = p.connect() pc2 = p.connect() pc3 = p.connect() pc1.close() pc2.close() pc3.close() for i in range(5): pc1 = p.connect() is_(pc1.connection, c3) pc1.close() pc1 = p.connect() is_(pc1.connection, c3) pc2 = p.connect() is_(pc2.connection, c2) pc2.close() pc3 = p.connect() is_(pc3.connection, c2) pc2 = p.connect() is_(pc2.connection, c1) pc2.close() pc3.close() pc1.close()
Example #9
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def test_dispose_closes_pooled(self): dbapi = MockDBAPI() p = pool.QueuePool( creator=dbapi.connect, pool_size=2, timeout=None, max_overflow=0 ) c1 = p.connect() c2 = p.connect() c1_con = c1.connection c2_con = c2.connection c1.close() eq_(c1_con.close.call_count, 0) eq_(c2_con.close.call_count, 0) p.dispose() eq_(c1_con.close.call_count, 1) eq_(c2_con.close.call_count, 0) # currently, if a ConnectionFairy is closed # after the pool has been disposed, there's no # flag that states it should be invalidated # immediately - it just gets returned to the # pool normally... c2.close() eq_(c1_con.close.call_count, 1) eq_(c2_con.close.call_count, 0) # ...and that's the one we'll get back next. c3 = p.connect() assert c3.connection is c2_con
Example #10
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def test_notify_waiters(self): dbapi = MockDBAPI() canary = [] def creator(): canary.append(1) return dbapi.connect() p1 = pool.QueuePool( creator=creator, pool_size=1, timeout=None, max_overflow=0 ) def waiter(p): conn = p.connect() canary.append(2) time.sleep(0.5) conn.close() c1 = p1.connect() threads = [] for i in range(5): t = threading.Thread(target=waiter, args=(p1,)) t.start() threads.append(t) time.sleep(0.5) eq_(canary, [1]) # this also calls invalidate() # on c1 p1._invalidate(c1) for t in threads: t.join(join_timeout) eq_(canary, [1, 1, 2, 2, 2, 2, 2])
Example #11
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def test_fifo(self): c1, c2, c3 = Mock(), Mock(), Mock() connections = [c1, c2, c3] def creator(): return connections.pop(0) p = pool.QueuePool(creator) pc1 = p.connect() pc2 = p.connect() pc3 = p.connect() pc1.close() pc2.close() pc3.close() pc1 = p.connect() is_(pc1.connection, c1) pc1.close() pc1 = p.connect() is_(pc1.connection, c2) pc2 = p.connect() is_(pc2.connection, c3) pc2.close() pc3 = p.connect() is_(pc3.connection, c1) pc2 = p.connect() is_(pc2.connection, c3) pc2.close() pc3.close() pc1.close()
Example #12
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def _test_overflow(self, thread_count, max_overflow): reaper = testing.engines.ConnectionKiller() dbapi = MockDBAPI() mutex = threading.Lock() def creator(): time.sleep(0.05) with mutex: return dbapi.connect() p = pool.QueuePool( creator=creator, pool_size=3, timeout=2, max_overflow=max_overflow ) reaper.add_pool(p) peaks = [] def whammy(): for i in range(10): try: con = p.connect() time.sleep(0.005) peaks.append(p.overflow()) con.close() del con except tsa.exc.TimeoutError: pass threads = [] for i in range(thread_count): th = threading.Thread(target=whammy) th.start() threads.append(th) for th in threads: th.join(join_timeout) self.assert_(max(peaks) <= max_overflow) reaper.assert_all_closed()
Example #13
Source File: test_transaction.py From sqlalchemy with MIT License | 5 votes |
def test_per_connection(self): from sqlalchemy.pool import QueuePool eng = testing_engine( options=dict(poolclass=QueuePool, pool_size=2, max_overflow=0) ) c1 = eng.connect() c1 = c1.execution_options( isolation_level=self._non_default_isolation_level() ) c2 = eng.connect() eq_( eng.dialect.get_isolation_level(c1.connection), self._non_default_isolation_level(), ) eq_( eng.dialect.get_isolation_level(c2.connection), self._default_isolation_level(), ) c1.close() c2.close() c3 = eng.connect() eq_( eng.dialect.get_isolation_level(c3.connection), self._default_isolation_level(), ) c4 = eng.connect() eq_( eng.dialect.get_isolation_level(c4.connection), self._default_isolation_level(), ) c3.close() c4.close()
Example #14
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def test_listen_targets_per_subclass(self): """test that listen() called on a subclass remains specific to that subclass.""" canary = [] def listen_one(*args): canary.append("listen_one") def listen_two(*args): canary.append("listen_two") def listen_three(*args): canary.append("listen_three") event.listen(pool.Pool, "connect", listen_one) event.listen(pool.QueuePool, "connect", listen_two) event.listen(pool.SingletonThreadPool, "connect", listen_three) p1 = pool.QueuePool(creator=MockDBAPI().connect) p2 = pool.SingletonThreadPool(creator=MockDBAPI().connect) assert listen_one in p1.dispatch.connect assert listen_two in p1.dispatch.connect assert listen_three not in p1.dispatch.connect assert listen_one in p2.dispatch.connect assert listen_two not in p2.dispatch.connect assert listen_three in p2.dispatch.connect p1.connect() eq_(canary, ["listen_one", "listen_two"]) p2.connect() eq_(canary, ["listen_one", "listen_two", "listen_one", "listen_three"])
Example #15
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def _queuepool_dbapi_fixture(self, **kw): dbapi = MockDBAPI() return ( dbapi, pool.QueuePool(creator=lambda: dbapi.connect("foo.db"), **kw), )
Example #16
Source File: test_deprecations.py From sqlalchemy with MIT License | 5 votes |
def _queuepool_dbapi_fixture(self, **kw): dbapi = MockDBAPI() return ( dbapi, pool.QueuePool(creator=lambda: dbapi.connect("foo.db"), **kw), )
Example #17
Source File: engines.py From oslo.db with Apache License 2.0 | 5 votes |
def _init_connection_args( url, engine_args, max_pool_size=None, max_overflow=None, pool_timeout=None, **kw): pool_class = url.get_dialect().get_pool_class(url) if issubclass(pool_class, pool.QueuePool): if max_pool_size is not None: engine_args['pool_size'] = max_pool_size if max_overflow is not None: engine_args['max_overflow'] = max_overflow if pool_timeout is not None: engine_args['pool_timeout'] = pool_timeout
Example #18
Source File: test_persistence.py From palladium with Apache License 2.0 | 5 votes |
def test_init_poolclass_set(self, Database, request): from sqlalchemy.pool import QueuePool path = '/tmp/palladium.testing-{}.sqlite'.format(os.getpid()) request.addfinalizer(lambda: os.remove(path)) db = Database('sqlite:///{}'.format(path), poolclass=QueuePool) assert isinstance(db.engine.pool, QueuePool)
Example #19
Source File: orm.py From girlfriend with MIT License | 5 votes |
def setUp(self): config = Config({ "db_test": { "connect_url": "sqlite:///:memory:", "pool_policy": "test" }, "dbpool_test": { "poolclass": "QueuePool", "pool_size": 10, } }) _engine_manager.validate_config(config) _engine_manager.init_all(config) global Base engine_container = _engine_manager.engine("test") Base.metadata.create_all(engine_container.engine) student_sam = Student(id=1, name="Sam", fullname="SamChi", grade=1) student_jack = Student(id=2, name="Jack", fullname="JackMa", grade=2) student_betty = Student( id=3, name="Betty", fullname="Betty Smith", grade=1) grade1 = Grade(id=1, name="Grade One") grade2 = Grade(id=2, name="Grade Two") session = engine_container.session() session.add_all([ student_sam, student_jack, student_betty, grade1, grade2 ]) session.commit() self.config = config
Example #20
Source File: test_pool.py From sqlalchemy with MIT License | 5 votes |
def setup(self): # create a throwaway pool which # has the effect of initializing # class-level event listeners on Pool, # if not present already. p1 = QueuePool(creator=self.Connection, pool_size=3, max_overflow=-1) p1.connect() global pool pool = QueuePool(creator=self.Connection, pool_size=3, max_overflow=-1)
Example #21
Source File: test_pool.py From sqlalchemy with MIT License | 4 votes |
def test_timeout_race(self): # test a race condition where the initial connecting threads all race # to queue.Empty, then block on the mutex. each thread consumes a # connection as they go in. when the limit is reached, the remaining # threads go in, and get TimeoutError; even though they never got to # wait for the timeout on queue.get(). the fix involves checking the # timeout again within the mutex, and if so, unlocking and throwing # them back to the start of do_get() dbapi = MockDBAPI() p = pool.QueuePool( creator=lambda: dbapi.connect(delay=0.05), pool_size=2, max_overflow=1, timeout=3, ) timeouts = [] def checkout(): for x in range(1): now = time.time() try: c1 = p.connect() except tsa.exc.TimeoutError: timeouts.append(time.time() - now) continue time.sleep(4) c1.close() threads = [] for i in range(10): th = threading.Thread(target=checkout) th.start() threads.append(th) for th in threads: th.join(join_timeout) assert len(timeouts) > 0 for t in timeouts: assert t >= 3, "Not all timeouts were >= 3 seconds %r" % timeouts # normally, the timeout should under 4 seconds, # but on a loaded down buildbot it can go up. assert t < 14, "Not all timeouts were < 14 seconds %r" % timeouts
Example #22
Source File: test_pool.py From sqlalchemy with MIT License | 4 votes |
def test_waiters_handled(self): """test that threads waiting for connections are handled when the pool is replaced. """ mutex = threading.Lock() dbapi = MockDBAPI() def creator(): with mutex: return dbapi.connect() success = [] for timeout in (None, 30): for max_overflow in (0, -1, 3): p = pool.QueuePool( creator=creator, pool_size=2, timeout=timeout, max_overflow=max_overflow, ) def waiter(p, timeout, max_overflow): success_key = (timeout, max_overflow) conn = p.connect() success.append(success_key) time.sleep(0.1) conn.close() c1 = p.connect() # noqa c2 = p.connect() threads = [] for i in range(2): t = threading.Thread( target=waiter, args=(p, timeout, max_overflow) ) t.daemon = True t.start() threads.append(t) # this sleep makes sure that the # two waiter threads hit upon wait() # inside the queue, before we invalidate the other # two conns time.sleep(0.2) p._invalidate(c2) for t in threads: t.join(join_timeout) eq_(len(success), 12, "successes: %s" % success)
Example #23
Source File: test_pool.py From sqlalchemy with MIT License | 4 votes |
def test_hanging_connect_within_overflow(self): """test that a single connect() call which is hanging does not block other connections from proceeding.""" dbapi = Mock() mutex = threading.Lock() def hanging_dbapi(): time.sleep(2) with mutex: return dbapi.connect() def fast_dbapi(): with mutex: return dbapi.connect() creator = threading.local() def create(): return creator.mock_connector() def run_test(name, pool, should_hang): if should_hang: creator.mock_connector = hanging_dbapi else: creator.mock_connector = fast_dbapi conn = pool.connect() conn.operation(name) time.sleep(1) conn.close() p = pool.QueuePool(creator=create, pool_size=2, max_overflow=3) threads = [ threading.Thread(target=run_test, args=("success_one", p, False)), threading.Thread(target=run_test, args=("success_two", p, False)), threading.Thread(target=run_test, args=("overflow_one", p, True)), threading.Thread(target=run_test, args=("overflow_two", p, False)), threading.Thread( target=run_test, args=("overflow_three", p, False) ), ] for t in threads: t.start() time.sleep(0.2) for t in threads: t.join(timeout=join_timeout) eq_( dbapi.connect().operation.mock_calls, [ call("success_one"), call("success_two"), call("overflow_two"), call("overflow_three"), call("overflow_one"), ], )
Example #24
Source File: database.py From grimoirelab-sortinghat with GNU General Public License v3.0 | 4 votes |
def create_database_engine(user, password, database, host, port): """Create a database engine""" driver = 'mysql+pymysql' url = URL(driver, user, password, host, port, database, query={'charset': 'utf8mb4'}) # Generic parameters for the engine. # # SSL param needs a non-empty dict to be activated in pymsql. # That is why a fake parameter 'activate' is given but not # used by the library. # engine_params = { 'poolclass': QueuePool, 'pool_size': 25, 'pool_pre_ping': True, 'echo': False, 'connect_args': { 'ssl': { 'activate': True } } } engine = create_engine(url, **engine_params) try: engine.connect().close() except InternalError: # Try non-SSL connection engine_params['connect_args'].pop('ssl') engine = create_engine(url, **engine_params) engine.connect().close() return engine
Example #25
Source File: orm.py From girlfriend with MIT License | 4 votes |
def setUp(self): self.test_db_file = "/tmp/gftest.db" connection_url = "sqlite:///{}".format(self.test_db_file) self.config = Config({ "db_test": { "connect_url": "sqlite:///:memory:", "pool_policy": "test" }, "db_test2": { "connect_url": connection_url }, "dbpool_test": { "poolclass": "QueuePool", "pool_size": 10, "pool_recycle": 3600, "pool_timeout": 20 } }) conn = sqlite3.connect(self.test_db_file) create_table_sql = """ create table user ( id integer primary key, name varchar(10) unique, grade int not null, description text not null ) """ conn.execute(create_table_sql) conn.execute(( "insert into user (id, name, grade, description) values " "(1, 'SamChi', 1, 'I am SamChi')" )) conn.commit()