Skip to content

Instantly share code, notes, and snippets.

@amotl
Created July 1, 2024 16:24
Show Gist options
  • Save amotl/e39d8e6ac93c0c0c37746fdb7ec66784 to your computer and use it in GitHub Desktop.
Save amotl/e39d8e6ac93c0c0c37746fdb7ec66784 to your computer and use it in GitHub Desktop.
Access list of active server connections to CrateDB when using SQLAlchemy
# Connect using SQLAlchemy Core.
import sqlalchemy as sa
from pprint import pp
dburi = "crate://localhost:4200"
query = "SELECT country, mountain, coordinates, height FROM sys.summits ORDER BY country;"
def print_active_servers(connection: sa.engine.Connection):
print("Active Servers:", connection.connection.dbapi_connection.client.active_servers)
if __name__ == "__main__":
engine = sa.create_engine(dburi, echo=True)
with engine.connect() as connection:
print_active_servers(connection)
with connection.execute(sa.text(query)) as result:
pp(result.mappings().fetchall())
print_active_servers(connection)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment