GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing university.voltdb.com — 8.4. Python Client Interface Using VoltDB8.4. Python Client InterfaceVoltDB provides a client interface for programs written in Python. The Python client interface is packaged with VoltDB
and is also available from PyPI via The new VoltDB Python client interface offers similar functionality to the Java client interface. The
The following sections explain the four key steps when using the Python interface:
8.4.1. Creating a Connection to the Database ClusterBefore you can call VoltDB stored procedures, you must create a client instance and connect to the database cluster.
In the Python interface this is done through the class from voltclient import VoltClient client = VoltClient(hosts=["voltsvr1", "voltsvr2"]) You do not need to list every server in the cluster. If the client is given at least one reachable node, it finds the rest automatically. 8.4.2. Invoking Stored ProceduresThe Python client provides both a synchronous and asynchronous interface. In either case, to make stored procedure
calls, you must first import from voltclient import VoltType
response = client.call("Vote",
[VoltType.BIGINT, VoltType.INTEGER, VoltType.BIGINT],
[5551234567, 3, 2])8.4.3. Invoking Stored Procedures AsynchronouslyTo make asynchronous procedure calls, use import concurrent.futures
# Each vote is (phone number, contestant number)
votes = [
(6175550101, 1),
(6175550102, 3),
(2125550199, 1),
(2125550143, 6),
]
futures = [client.call_async("Vote",
[VoltType.BIGINT, VoltType.INTEGER, VoltType.BIGINT],
[num, contestant, 1])
for num, contestant in votes]
for f in concurrent.futures.as_completed(futures):
response = f.result()
# ... check each response ...8.4.4. Interpreting the ResultsBoth the synchronous and asynchronous invocations return a if response.status != 1:
print(f"Stored procedure failed: {response.statusString}")An error that prevents a call from completing at all, like the connection being lost, raises a Python exception. The outcome of the call is uncertain, and the client will not resend it. The application must decide whether it is safe to make the same call a second time. If the stored procedure is successful, you can use the client response to retrieve the results. The results are
returned as a list of response = client.call("Results")
if response.status != 1:
print("Results failed:", response.statusString)
else:
table = response.tables[0]
for row in table.tuples:
name, number, total = row
print(f"Contestant {number}, {name}: {total} votes."8.4.5. Closing the Connections to the Database ClusterThe client runs background threads for its connections, so it is important to close it when your application is done.
This is done manually by calling client = VoltClient(hosts=["voltsvr1", "voltsvr2"]) try: ... # make procedure calls finally: client.close() Alternatively, you can use the client as a context manager with the Python with VoltClient(hosts=["voltsvr1", "voltsvr2"]) as client ... # make procedure calls When you make asynchronous calls, it is important to ensure all calls complete before closing the connection. The
following example uses try: futures = [client.call_async(...) for ... in ...] finally: concurrent.futures.wait(futures) # returns when every call has completed client.close() # safely closes the connection |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |