DBInteractionPool

While the DBConnectionPool resembles a simple, “classic” connection pool, the DBInteractionPool is sort of a meta or management pool of one or more DBConnectionPool instances.

The idea behind this structure is to support master slave replicated database cluster backends that manage write and read-only (without side-effects) operations on different machines/clusters.

DBInteractionPool provides two main methods for interaction:

  • DBInteractionPool.run() to run queries (interactions) on the pool
  • DBInteractionPool.listen() to subscribe to asyncronous event channels from the database

Class Documenation

class gdbpool.interaction_pool.DBInteractionPool(dsn, pool_size=10, pool_name='default', do_log=False)[source]

Bases: object

The DBInteractionPool manages DBConnectionPool instances and can run queries or functions (ie. several queries wrapped in a function) on one of these pools.

Parameters:
  • dsn (string) – DSN for the default class:DBConnectionPool
  • pool_size (int) – Poolsize of the first/default class:DBConnectionPool
  • pool_name (string) – Keyname for the first/default class:DBConnectionPool
  • do_log (bool) – Log to the console or not
add_pool(dsn=None, pool_name=None, pool_size=10, default_write_pool=False, default_read_pool=False, default_pool=False, db_module='psycopg2')[source]

Add a named :class:DBConnectionPool

Parameters:
  • dsn (string) – dsn
  • pool_name (string) – a name for the pool to identify it inside the DBInteractionPool
  • pool_size (int) – Number of connections the pool should have.
  • default_write_pool (bool) – Should the added pool used as the default pool for write operations?
  • default_read_pool (bool) – Should the added pool used as the default pool for read operations?
  • default_pool (bool) – Should the added pool used as the default pool? (must be a write pool)
  • db_module (string) – name of the DB-API module to use

Note

db_module right now ONLY supports psycopg2 and the option most likely will be removed in the future

listen_on(result_queue=None, channel_name=None, pool=None, cancel_event=None, sleep_cycle=0.1)[source]

Listen for asynchronous events on a named Channel and pass them to the result_queue

Parameters:
  • result_queue (gevent.Queue) – The gevent.Queue to pass event payloads to
  • channel_name (string) – Name of the channel to LISTEN on
  • pool (string) – Name of the pool to get the connection from
  • cancel_event (gevent.Event) – A gevent.Event which will break the listening loop when set
run(interaction=None, interaction_args=None, get_result=True, is_write=True, pool=None, conn=None, cursor=None, partial_txn=False, dry_run=False, *args, **kwargs)[source]

Run an interaction on one of the managed :class:DBConnectionPool pools.

Parameters:
  • interaction (function|method) – The interaction to run. Either a SQL string or a function that takes at least a parameter conn.
  • interaction_args (string) – None,
  • get_result (bool) – call and return cursor.fetchall() when True - otherwise just return True as result if no exception was raised.
  • is_write (bool) – If the interaction has no side-effects set to False. Without naming a pool the default_read pool would be used.
  • pool (string) – Keyname of the pool to get the a connection from
  • conn (connection) – Pass in a Connection instead of getting one from the pool. (ie. for locks in transactions that span several interactions. Use partial_txn = True to retrieve the Connection and then pass it into the next interaction run.)
  • cursor (cursor) – Pass in a Cursor instead of getting one from the Connection (ie. for locks in transactions that span several interactions. Use partial_txn = True to retrieve the Cursor and then pass it into the next interaction run.)
  • partial_txn (bool) – Return connection and cursor after executing the interaction (ie. for locks in transactions that span several interactions)
  • dry_run (bool) – Run the query with mogrify instead of execute and output the query that would have run. (Only applies to query interactions)
  • args (list) – positional args for the interaction
  • kwargs (dict) – kwargs for the interaction
Return type:

gevent.AsyncResult

Returns:

– a gevent.AsyncResult that will hold the result of the interaction once it finished. When partial_txn = True it will return a dict that will hold the result, the connection, and the cursor that ran the transaction. (use for locking with SELECT FOR UPDATE)

Table Of Contents

Previous topic

PoolConnection

Next topic

PoolConnection

This Page