For high-concurrency applications, use pydgraph.AsyncDgraphClient. This uses asyncio and grpc.aio for true asynchronous operations.
Basic Async Usage
import asyncio
import pydgraph
async def main():
client_stub = pydgraph.AsyncDgraphClientStub('localhost:9080')
client = pydgraph.AsyncDgraphClient(client_stub)
try:
await client.login("groot", "password")
# Mutation
txn = client.txn()
await txn.mutate(set_obj={"name": "Alice"}, commit_now=True)
# Query
txn = client.txn(read_only=True)
response = await txn.query('{ me(func: has(name)) { name } }')
print(response.json)
finally:
await client.close()
asyncio.run(main())
Async Connection Strings
async with await pydgraph.async_open("dgraph://localhost:9080") as client:
version = await client.check_version()
print(version)
Async Context Managers
Both the client and transactions support async with for automatic cleanup:
async with await pydgraph.async_open("dgraph://localhost:9080") as client:
await client.login("groot", "password")
async with client.txn() as txn:
response = await txn.query('{ me(func: has(name)) { name } }')
print(response.json)
async with await pydgraph.async_open("dgraph://localhost:9080") as client:
await client.login("groot", "password")
async with client.txn() as txn:
res = await txn.query("{ me(func: has(name)) { name } }")
print(res.json)