AnyBlok Multi Engines framework

Registry plugin


class anyblok_multi_engines.registry.MixinSession

Bases: object

Mixin for the SQLAlchemy session the goal is to allow the connection with more than one engine: masters / slaves engines

get_bind(mapper=None, clause=None)

Overload the Session.get_bind method of SQLAlchemy

the rule are:

  • if has session_connection: use the session_connection, because it use during the creation of the AnyBlok Registry, which need get the same bind
  • if unittest_transaction: durring unittest, they are no slaves / masters
  • if flushing: write on the database then we use the master
  • read the database then use a slave

Mixin of the registry

class anyblok_multi_engines.registry.MultiEngines

Bases: object

Mixin class which overload the AnyBlok Registry class

the goal is to implement in the the Registry the masters / slaves behaviour


Return the bind in function of engine


Overwrite close to cloe all the engines


Overwrite the creation of Session factory to Use the multi binding


Return the engine


Return one engine among the engines

Parameters:ro – if True the engine will be read only else write only
Return type:engine

Initialise the bind for unittest


Overload the initiation of engine to create more than one engine

use the Configuration option to create engines:

  • db_url: read and write engine
  • db_ro_urls: read only engines (list)
  • db_wo_url: write only engines


All the engines use the same database name not at the same location

Parameters:db_name – name of the database for the engines

Plugin class

class anyblok_multi_engines.registry.RegistryMultiEngines(db_name, loadwithoutmigration=False, unittest=False, **kwargs)

Bases: anyblok_multi_engines.registry.MultiEngines, anyblok.registry.Registry

Pluging class

get_url plugin

anyblok_multi_engines.config.get_url(db_name=None, url=None)

Return an sqlalchemy URL for database

  • db_name – Name of the database
  • url – base of the url
Return type:

SqlAlchemy URL