Pool¶
The library provides connection pool as well as plain
Connection objects.
The basic usage is:
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
@asyncio.coroutine
def go()
pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='mysql', loop=loop, autocommit=False)
with (yield from pool) as conn:
cur = yield from conn.cursor()
yield from cur.execute("SELECT 10")
# print(cur.description)
(r,) = yield from cur.fetchone()
assert r == 10
pool.close()
yield from pool.wait_closed()
loop.run_until_complete(go())
-
create_pool(minsize=1, maxsize=10, loop=None, **kwargs)¶ A coroutine that creates a pool of connections to MySQL database.
- Parameters
minsize (int) – minimum sizes of the pool.
maxsize (int) – maximum sizes of the pool.
loop – is an optional event loop instance,
asyncio.get_event_loop()is used if loop is not specified.echo (bool) – – executed log SQL queryes (
Falseby default).kwargs – The function accepts all parameters that
aiomysql.connect()does plus optional keyword-only parameters loop, minsize, maxsize.
- Returns
Poolinstance.
-
class
Pool¶ A connection pool.
After creation pool has minsize free connections and can grow up to maxsize ones.
If minsize is
0the pool doesn’t creates any connection on startup.If maxsize is
0than size of pool is unlimited (but it recycles used connections of course).The most important way to use it is getting connection in with statement:
with (yield from pool) as conn: cur = yield from conn.cursor()
See also
Pool.acquire()andPool.release()for acquringConnectionwithout with statement.-
echo¶ Return echo mode status. Log all executed queries to logger named
aiomysqlifTrue
-
minsize¶ A minimal size of the pool (read-only),
1by default.
-
maxsize¶ A maximal size of the pool (read-only),
10by default.
-
size¶ A current size of the pool (readonly). Includes used and free connections.
-
freesize¶ A count of free connections in the pool (readonly).
-
close()¶ Close pool.
Mark all pool connections to be closed on getting back to pool. Closed pool doesn’t allow to acquire new connections.
If you want to wait for actual closing of acquired connection please call
wait_closed()afterclose().Warning
The method is not a coroutine.
-
terminate()¶ Terminate pool.
Close pool with instantly closing all acquired connections also.
wait_closed()should be called afterterminate()for waiting for actual finishing.Warning
The method is not a coroutine.
-
wait_closed()¶ A coroutine that waits for releasing and closing all acquired connections.
Should be called after
close()for waiting for actual pool closing.
-
acquire()¶ A coroutine that acquires a connection from free pool. Creates new connection if needed and
sizeof pool is less thanmaxsize.Returns a
Connectioninstance.
-
release(conn)¶ Reverts connection conn to free pool for future recycling.
Warning
The method is not a coroutine.
-