Python - Multiple Error during Load testing

Hie, I have an created API in Django

class testAPI(APIView):

    class InputSerializer(serializers.Serializer):

        name = serializers.CharField(required=True)

    serializer_class = InputSerializer

    def post(self, request):
        serializer = self.InputSerializer(data=request.data)
        try:
            serializer.is_valid(raise_exception=True)
        except serializers.ValidationError as e:
            return format_response(HTTP_400_BAD_REQUEST, errors=e.detail)
        data = serializer.validated_data
        return callingmyfunc(data["name"])
def callingmyfunc(name):
    out = session.run(
        f"""
        MATCH (p:Label1)<--(o:Label2)
        <--(i:Label3s)-[v]->(s:Label4s)-[q]->(a:Label5s)
        MATCH (p:Label1)<--(o:Label2)<--
        (i:Label3s)-[t]->(r:Label6s)
        WHERE p.name = "{name}"
        WITH o,p,q,r,s,t,a,i,v
            ORDER BY t.order, v.order, q.order
        WITH
            {{Label5: a.name, Label5Id: a.Id,
              docId: a.docId , Type: a.type,
              Label5Order: q.order}} AS arts,
              o,p,s,v,i,t,r
        WITH
            {{Label3: i.name,
            Label3Id: i.Id}} AS Label3,
            {{TemplateName: o.name,
            TemplateId: o.Id}} AS template,
            {{Label6: r.name, Label6Id: r.Id}} AS Label6,
            {{Label4: s.name,
            Label4Id: s.Id,
            Label5s:collect(arts)}} AS delta
        WITH
            Label3, template,
            apoc.coll.toSet(collect(Label6)) AS Label6s,
            apoc.coll.toSet(collect(delta)) AS alpha
        RETURN {{Label3:Label3,
            Label2: template,
            Label6s: Label6s,
            Label4s: alpha}} AS _"""
    ).data()

    output = [x["_"] for x in out]
    session.close()
    return Response({"Output": output}, status=200)

When I load test it, I get multiple errors like "Generator already executing", "Buffer Error", and weirdly sometime "AttributeError". Sorry for the weird names in the code. Would be glad if someone can help me find the problem in the code, Thanks in advance.

As posted here, I think your issue is that you're sharing the driver's session object across threads. This is undefined behavior as mentioned in the API docs:

Session creation is a lightweight operation and sessions are not thread safe .

The code snippets you've shared don't really show how the session is created, which makes it hard to pinpoint at where things are going wrong.

1 Like

Sorry for deleting the reply in the other thread, well this is how I have my session created. Guess this exactly how we are not supposed to create a sesssion right.

driver = GraphDatabase.driver(uri=NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))
session = driver.session()

That is fine. What @rouven_bauer is referring to is your session variable in your callingmyfunction method. Is that using the same session value each time this method is called in your load test? That will be a problem if they are called concurrently, which I assume would occur since you are load testing.

Instead, use the same driver throughout your tests, but call driver.session() in each method invocation to get a new session for each session.run().

2 Likes