Closed interrogator closed 11 months ago
Asyncpg's equivalent of open
is await
-ing on it (or using it as an async context manager), so if you want to delay the initialization, simply delay the await
part, e.g:
pool_config = asyncpg.Pool(...)
...
# some time after
pool = await pool_config
# or
async with pool_config as pool:
...
Does that work for you?
My app uses
rq
, which spawns worker processes viaos.fork()
. I have a fairly typical use-case: I want a connection pool to be shared by all workers and connections acquired by the main app from time to time. But for this to work, the connection needs to be opened after the fork takes place, or else all kinds of errors spring up when two different workers access the same connection.With
psycopg
'sAsyncConnectionPool
, there is anopen()
async method that can be run after forking. Calling it again subsequently does nothing, which is fine. Withasyncpg
however there doesn't seem to be such a simple interface. Instead I needed to set_initialized
to False, call_initialize()
or_async__init_()
or similar. The logics for instantiating the pool and opening it are somewhat bound together.It would be great if the connection pool had the ability to delay opening connection till something like
open()
is called. Right now, short or just recreating the entire pool in each worker process, I can't find a way of making it work.Any plans to add something like this? Or at least, an explanation of how this would best be accomplished with the current version?
Thanks for your work on
asyncpg
!