-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_ipc_client.py
More file actions
62 lines (48 loc) · 1.8 KB
/
08_ipc_client.py
File metadata and controls
62 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""
Example 08: IPC Client - Multi-process access via Unix socket
NOTE: This example requires a running SochDB IPC server.
Start the server first: cargo run --bin ipc_server -- --socket /tmp/sochdb.sock
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
from sochdb import IpcClient, Query
from sochdb.errors import ConnectionError
SOCKET_PATH = "/tmp/sochdb.sock"
def main():
print("=" * 60)
print("SochDB - Example 08: IPC Client")
print("=" * 60)
try:
client = IpcClient.connect(SOCKET_PATH, timeout=5.0)
print(f"✓ Connected to {SOCKET_PATH}")
except ConnectionError as e:
print(f"✗ Could not connect: {e}")
print("\nTo run this example, start the IPC server first:")
print(" cargo run --bin ipc_server -- --socket /tmp/sochdb.sock")
return
try:
# Ping
latency = client.ping()
print(f"✓ Ping: {latency*1000:.2f}ms")
# Put/Get
client.put(b"hello", b"world")
value = client.get(b"hello")
print(f"✓ Put/Get: hello={value.decode()}")
# Path API
client.put_path(["users", "alice", "email"], b"alice@example.com")
email = client.get_path(["users", "alice", "email"])
print(f"✓ Path: users/alice/email={email.decode()}")
# Transaction
txn_id = client.begin_transaction()
# Note: IPC transactions require passing txn_id to operations
commit_ts = client.commit(txn_id)
print(f"✓ Transaction committed: {commit_ts}")
# Stats
stats = client.stats()
print(f"✓ Stats: {stats}")
finally:
client.close()
print("✓ Connection closed")
if __name__ == "__main__":
main()