cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4j and FastApi concurrency

kaptenh
Node Clone

Hi!
I am trying make a fastapi app using the neo4j python driver (4.2.0) to connect to neo.
I am not very successful, as queries from one user block the queries from other users and I cant figure out why. Any helpful suggestions?

My code looks something like this:



from neo4j import GraphDatabase
from fastapi impor t FastAPI
neo4j_uri, username, password = get_neo_parameters()
app = FastAPI()

def get_driver(uri, username, password):
    driver = GraphDatabase.driver(uri, auth=(username, password))
    return driver

def run_graph_queries(uri, username, password, queries):
    driver = get_driver(uri, username, password)
    with driver.session() as session:
        r = session.read_transaction(do_graph_queries, queries)
        return r


def do_graph_query(tx, queries):
    result = []
    for query in queries:
        res = tx.run(query['query'], query['parameters'])
        result.append(res.graph())
    return result


app.run_graph_queries = lamba x: run_graph_queries(neo4j_uri, username, password, x)


uvicorn.run("app", port=9999)

and then my endpoints calls request.app.run_graph_queries(queries) with queries from a POST.

I would be very grateful if someone could explain to me why this blocks all users..

Thanks

2 REPLIES 2

I haven't used uvicorn before, but does it do multi-threading by default?

kaptenh
Node Clone

yeah.. it was all my fault and none of it was neos fault 🙂