Connection pooling
sparqlite automatically reuses HTTP connections across multiple requests to the same endpoint, reducing latency and improving performance.
How it works
Section titled “How it works”When you create a SPARQLClient, it initializes a persistent connection that is reused for all subsequent requests. This avoids the overhead of establishing a new TCP connection and TLS handshake for each query.
from sparqlite import SPARQLClient
# Connection is established on first requestclient = SPARQLClient("https://dbpedia.org/sparql")
# First query - establishes connectionresult1 = client.query("SELECT ?s WHERE { ?s ?p ?o } LIMIT 1")
# Subsequent queries - reuse existing connection (faster)result2 = client.query("SELECT ?p WHERE { ?s ?p ?o } LIMIT 1")result3 = client.query("SELECT ?o WHERE { ?s ?p ?o } LIMIT 1")
# Clean up when doneclient.close()Using a context manager
Section titled “Using a context manager”The recommended way to use sparqlite is with a context manager, which automatically closes the connection when done:
from sparqlite import SPARQLClient
with SPARQLClient("https://dbpedia.org/sparql") as client: result1 = client.query("SELECT ?s WHERE { ?s ?p ?o } LIMIT 1") result2 = client.query("SELECT ?p WHERE { ?s ?p ?o } LIMIT 1") # Connection is automatically closed when exiting the with blockCleaning up resources
Section titled “Cleaning up resources”Using close()
Section titled “Using close()”If you are not using a context manager, call close() when you are done with the client:
client = SPARQLClient("https://dbpedia.org/sparql")try: result = client.query("SELECT * WHERE { ?s ?p ?o } LIMIT 5")finally: client.close()Automatic cleanup
Section titled “Automatic cleanup”If you forget to call close(), the connection will be closed when the client object is garbage collected. However, it is best practice to explicitly close connections to release resources promptly.
Thread safety
Section titled “Thread safety”Each SPARQLClient instance should be used by a single thread. If you need to make concurrent requests, create separate client instances for each thread.