@@ -26,13 +26,14 @@ class Pool:
26
26
'_connect_args' , '_connect_kwargs' ,
27
27
'_working_addr' , '_working_opts' ,
28
28
'_con_count' , '_max_queries' , '_connections' ,
29
- '_initialized' , '_closed' , '_setup' )
29
+ '_initialized' , '_closed' , '_setup' , '_init' )
30
30
31
31
def __init__ (self , * connect_args ,
32
32
min_size ,
33
33
max_size ,
34
34
max_queries ,
35
35
setup ,
36
+ init ,
36
37
loop ,
37
38
** connect_kwargs ):
38
39
@@ -57,6 +58,7 @@ def __init__(self, *connect_args,
57
58
self ._max_queries = max_queries
58
59
59
60
self ._setup = setup
61
+ self ._init = init
60
62
61
63
self ._connect_args = connect_args
62
64
self ._connect_kwargs = connect_kwargs
@@ -88,10 +90,13 @@ async def _new_connection(self):
88
90
loop = self ._loop ,
89
91
** self ._working_opts )
90
92
93
+ if self ._init is not None :
94
+ await self ._init (con )
95
+
91
96
self ._connections .add (con )
92
97
return con
93
98
94
- async def _init (self ):
99
+ async def _initialize (self ):
95
100
if self ._initialized :
96
101
return
97
102
if self ._closed :
@@ -219,10 +224,10 @@ def _reset(self):
219
224
self ._queue = asyncio .Queue (maxsize = self ._maxsize , loop = self ._loop )
220
225
221
226
def __await__ (self ):
222
- return self ._init ().__await__ ()
227
+ return self ._initialize ().__await__ ()
223
228
224
229
async def __aenter__ (self ):
225
- await self ._init ()
230
+ await self ._initialize ()
226
231
return self
227
232
228
233
async def __aexit__ (self , * exc ):
@@ -261,6 +266,7 @@ def create_pool(dsn=None, *,
261
266
max_size = 10 ,
262
267
max_queries = 50000 ,
263
268
setup = None ,
269
+ init = None ,
264
270
loop = None ,
265
271
** connect_kwargs ):
266
272
r"""Create a connection pool.
@@ -296,16 +302,22 @@ def create_pool(dsn=None, *,
296
302
:param int max_size: Max number of connections in the pool.
297
303
:param int max_queries: Number of queries after a connection is closed
298
304
and replaced with a new connection.
299
- :param coroutine setup: A coroutine to initialize a connection right before
305
+ :param coroutine setup: A coroutine to prepare a connection right before
300
306
it is returned from :meth:`~pool.Pool.acquire`.
301
307
An example use case would be to automatically
302
308
set up notifications listeners for all connections
303
309
of a pool.
310
+ :param coroutine init: A coroutine to initialize a connection when it
311
+ is created. An example use case would be to setup
312
+ type codecs with
313
+ :meth:`~asyncpg.connection.Connection.\
314
+ set_builtin_type_codec` or :meth:`~asyncpg.\
315
+ connection.Connection.set_type_codec`.
304
316
:param loop: An asyncio event loop instance. If ``None``, the default
305
317
event loop will be used.
306
318
:return: An instance of :class:`~asyncpg.pool.Pool`.
307
319
"""
308
320
return Pool (dsn ,
309
321
min_size = min_size , max_size = max_size ,
310
- max_queries = max_queries , loop = loop , setup = setup ,
322
+ max_queries = max_queries , loop = loop , setup = setup , init = init ,
311
323
** connect_kwargs )
0 commit comments