Run concurrent transactions
The driver is compatible with Python’s asyncio
, which allows implementing concurrent workflows.
To interact with the database in an asynchronous way, create an AsyncDriver
with AsyncGraphDatabase.driver()
.
The workflow is very similar to the synchronous version, except that you must use await
on all async calls, and define as async
all functions that should be awaited.
If you need causal consistency across different transactions, use bookmarks.
execute_query
import asyncio
from neo4j import AsyncGraphDatabase
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")
async def main():
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
records, summary, keys = await driver.execute_query(
"MATCH (a:Person) RETURN a.name AS name",
database_="neo4j"
)
names = [record["name"] for record in records]
print(names)
if __name__ == "__main__":
asyncio.run(main())
import asyncio
from neo4j import AsyncGraphDatabase
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")
async def main():
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
async with driver.session(database="neo4j") as session:
records = await session.execute_read(get_people)
print(records)
async def get_people(tx):
result = await tx.run("MATCH (a:Person) RETURN a.name AS name")
records = await result.values()
return records
if __name__ == "__main__":
asyncio.run(main())
Async implements a concurrency model, but it is not the only possible one.
Multithreading is also possible, although asyncio is often easier to implement in an application.
|
There is a known issue with Python 3.8 and the async driver where it gradually slows down. It is generally recommended to use the latest supported version of Python for the best performance, stability, and security. |
Glossary
- LTS
-
A Long Term Support release is one guaranteed to be supported for a number of years. Neo4j 4.4 is LTS, and Neo4j 5 will also have an LTS version.
- Aura
-
Aura is Neo4j’s fully managed cloud service. It comes with both free and paid plans.
- Cypher
-
Cypher is Neo4j’s graph query language that lets you retrieve data from the database. It is like SQL, but for graphs.
- APOC
-
Awesome Procedures On Cypher (APOC) is a library of (many) functions that can not be easily expressed in Cypher itself.
- Bolt
-
Bolt is the protocol used for interaction between Neo4j instances and drivers. It listens on port 7687 by default.
- ACID
-
Atomicity, Consistency, Isolation, Durability (ACID) are properties guaranteeing that database transactions are processed reliably. An ACID-compliant DBMS ensures that the data in the database remains accurate and consistent despite failures.
- eventual consistency
-
A database is eventually consistent if it provides the guarantee that all cluster members will, at some point in time, store the latest version of the data.
- causal consistency
-
A database is causally consistent if read and write queries are seen by every member of the cluster in the same order. This is stronger than eventual consistency.
- NULL
-
The null marker is not a type but a placeholder for absence of value. For more information, see Cypher → Working with
null
. - transaction
-
A transaction is a unit of work that is either committed in its entirety or rolled back on failure. An example is a bank transfer: it involves multiple steps, but they must all succeed or be reverted, to avoid money being subtracted from one account but not added to the other.
- backpressure
-
Backpressure is a force opposing the flow of data. It ensures that the client is not being overwhelmed by data faster than it can handle.
- transaction function
-
A transaction function is a callback executed by an
execute_read
orexecute_write
call. The driver automatically re-executes the callback in case of server failure. - Driver
-
A
Driver
object holds the details required to establish connections with a Neo4j database.