Created
July 1, 2024 16:24
-
-
Save amotl/e39d8e6ac93c0c0c37746fdb7ec66784 to your computer and use it in GitHub Desktop.
Access list of active server connections to CrateDB when using SQLAlchemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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