Hi everyone,
I'm a Python developer who is new to Neo4j. I'm interested in using async sessions to interact with the database, but I'm not sure if this is possible.
Is it possible to use async sessions with Neo4j in Python? If so, could you please provide some guidance on how to do this
If with async session you mean async as in the Python keyword and asyncio
the package, then yes, this is possible. Generally speaking, the async driver API is very closely mirroring the sync API (plus the need to add await
and async
here and there). So you can use pretty much any learning material you fancy to get started with Python and Neo4j and easily translate it to async.
E.g., GraphAcademy, the driver manual
There are also the API docs which highlight the difference between the synchronous and asynchronous APIs: sync vs async.
Real quick example to go from sync to async:
from neo4j import (
AsyncGraphDatabase,
GraphDatabase,
)
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "pass")
def sync_main():
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
print(
driver.execute_query("RETURN 1 AS n")
)
async def async_main():
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
await driver.verify_connectivity()
print(
await driver.execute_query("RETURN 1 AS n")
)
if __name__ == '__main__':
sync_main()
import asyncio
asyncio.run(async_main())
1 Like
Thank you so much for your detailed and helpful answer! Your explanations were very clear and helped me to solve the problem of connecting to the database.
I really appreciate your help.
1 Like