picopyn.client
1from .pool import Pool 2 3class Client: 4 """ 5 Async client for managing connections to a picodata cluster using a connection pool. 6 7 This client handles connection pooling, automatic node discovery (if enabled), 8 and supports load balancing strategies for query distribution. 9 10 :param dsn (str): The data source name (e.g., "postgresql://user:pass@host:port") for the cluster. 11 :param balance_strategy (callable, optional): A custom strategy function to select a connection 12 from the pool. If None, round-robin strategy is used. 13 :param connect_kwargs: Additional keyword arguments passed to each connection. 14 15 Example: 16 >>> def random_strategy(connections): 17 ... import random 18 ... return random.choice(connections) 19 20 >>> client = Client( 21 ... dsn="postgresql://admin:pass@localhost:5432", 22 ... balance_strategy=random_strategy 23 ... ) 24 """ 25 def __init__( 26 self, 27 dsn: str, 28 balance_strategy=None, 29 **connect_kwargs 30 ): 31 self._pool = Pool( 32 dsn=dsn, 33 max_size=10, 34 enable_discovery=True, 35 balance_strategy=balance_strategy, 36 **connect_kwargs 37 ) 38 39 async def connect(self): 40 """ 41 Prepares the client by connection connection pool. 42 43 This should be called before using the client to ensure connections are available. 44 """ 45 await self._pool.connect() 46 47 async def execute(self, query: str, *args): 48 """ 49 Executes a query that does not return rows (e.g. INSERT, UPDATE, DELETE). 50 51 :param query: The SQL query string. 52 :param args: Optional parameters for the SQL query. 53 :return: The result of the query execution. 54 """ 55 return await self._pool.execute(query, *args) 56 57 async def fetch(self, query: str, *args): 58 """ 59 Executes a query and fetches all resulting rows. 60 61 :param query: The SQL query string. 62 :param args: Optional parameters for the SQL query. 63 :return: A list of rows returned by the query. 64 """ 65 return await self._pool.fetch(query, *args) 66 67 async def fetchrow(self, query: str, *args): 68 """ 69 Executes a query and fetches a single row (first row). 70 71 :param query: The SQL query string. 72 :param args: Optional parameters for the SQL query. 73 :return: A single row returned by the query. 74 """ 75 return await self._pool.fetchrow(query, *args) 76 77 async def close(self): 78 """ 79 Closes all connections in the pool. 80 81 This should be called during application shutdown to clean up resources. 82 """ 83 await self._pool.close()
4class Client: 5 """ 6 Async client for managing connections to a picodata cluster using a connection pool. 7 8 This client handles connection pooling, automatic node discovery (if enabled), 9 and supports load balancing strategies for query distribution. 10 11 :param dsn (str): The data source name (e.g., "postgresql://user:pass@host:port") for the cluster. 12 :param balance_strategy (callable, optional): A custom strategy function to select a connection 13 from the pool. If None, round-robin strategy is used. 14 :param connect_kwargs: Additional keyword arguments passed to each connection. 15 16 Example: 17 >>> def random_strategy(connections): 18 ... import random 19 ... return random.choice(connections) 20 21 >>> client = Client( 22 ... dsn="postgresql://admin:pass@localhost:5432", 23 ... balance_strategy=random_strategy 24 ... ) 25 """ 26 def __init__( 27 self, 28 dsn: str, 29 balance_strategy=None, 30 **connect_kwargs 31 ): 32 self._pool = Pool( 33 dsn=dsn, 34 max_size=10, 35 enable_discovery=True, 36 balance_strategy=balance_strategy, 37 **connect_kwargs 38 ) 39 40 async def connect(self): 41 """ 42 Prepares the client by connection connection pool. 43 44 This should be called before using the client to ensure connections are available. 45 """ 46 await self._pool.connect() 47 48 async def execute(self, query: str, *args): 49 """ 50 Executes a query that does not return rows (e.g. INSERT, UPDATE, DELETE). 51 52 :param query: The SQL query string. 53 :param args: Optional parameters for the SQL query. 54 :return: The result of the query execution. 55 """ 56 return await self._pool.execute(query, *args) 57 58 async def fetch(self, query: str, *args): 59 """ 60 Executes a query and fetches all resulting rows. 61 62 :param query: The SQL query string. 63 :param args: Optional parameters for the SQL query. 64 :return: A list of rows returned by the query. 65 """ 66 return await self._pool.fetch(query, *args) 67 68 async def fetchrow(self, query: str, *args): 69 """ 70 Executes a query and fetches a single row (first row). 71 72 :param query: The SQL query string. 73 :param args: Optional parameters for the SQL query. 74 :return: A single row returned by the query. 75 """ 76 return await self._pool.fetchrow(query, *args) 77 78 async def close(self): 79 """ 80 Closes all connections in the pool. 81 82 This should be called during application shutdown to clean up resources. 83 """ 84 await self._pool.close()
Async client for managing connections to a picodata cluster using a connection pool.
This client handles connection pooling, automatic node discovery (if enabled), and supports load balancing strategies for query distribution.
Parameters
- dsn (str): The data source name (e.g., "postgresql://user:pass@host: port") for the cluster.
- balance_strategy (callable, optional): A custom strategy function to select a connection from the pool. If None, round-robin strategy is used.
- connect_kwargs: Additional keyword arguments passed to each connection.
Example:
def random_strategy(connections): ... import random ... return random.choice(connections)
>>> client = Client( ... dsn="postgresql://admin:pass@localhost:5432", ... balance_strategy=random_strategy ... )
40 async def connect(self): 41 """ 42 Prepares the client by connection connection pool. 43 44 This should be called before using the client to ensure connections are available. 45 """ 46 await self._pool.connect()
Prepares the client by connection connection pool.
This should be called before using the client to ensure connections are available.
48 async def execute(self, query: str, *args): 49 """ 50 Executes a query that does not return rows (e.g. INSERT, UPDATE, DELETE). 51 52 :param query: The SQL query string. 53 :param args: Optional parameters for the SQL query. 54 :return: The result of the query execution. 55 """ 56 return await self._pool.execute(query, *args)
Executes a query that does not return rows (e.g. INSERT, UPDATE, DELETE).
Parameters
- query: The SQL query string.
- args: Optional parameters for the SQL query.
Returns
The result of the query execution.
58 async def fetch(self, query: str, *args): 59 """ 60 Executes a query and fetches all resulting rows. 61 62 :param query: The SQL query string. 63 :param args: Optional parameters for the SQL query. 64 :return: A list of rows returned by the query. 65 """ 66 return await self._pool.fetch(query, *args)
Executes a query and fetches all resulting rows.
Parameters
- query: The SQL query string.
- args: Optional parameters for the SQL query.
Returns
A list of rows returned by the query.
68 async def fetchrow(self, query: str, *args): 69 """ 70 Executes a query and fetches a single row (first row). 71 72 :param query: The SQL query string. 73 :param args: Optional parameters for the SQL query. 74 :return: A single row returned by the query. 75 """ 76 return await self._pool.fetchrow(query, *args)
Executes a query and fetches a single row (first row).
Parameters
- query: The SQL query string.
- args: Optional parameters for the SQL query.
Returns
A single row returned by the query.
78 async def close(self): 79 """ 80 Closes all connections in the pool. 81 82 This should be called during application shutdown to clean up resources. 83 """ 84 await self._pool.close()
Closes all connections in the pool.
This should be called during application shutdown to clean up resources.