sqlalchemy.orm.exc.DetachedInstanceError - concurency problem in Pyramid with waitress

35 Views Asked by At

I have a simple query loading records from view (PostgreSQL):

    rows = DBSession.query(MyView).all()

next, I'm processing rows, copying them to rows1:

    for row in rows:
        # some calculation...
        rows1.append( row )

and finally, returning result to jinja2 renderer:

    return {"rows": rows1}

When there are few concurrent requests, some of them fails during jinja rendering with this error:

    sqlalchemy.orm.exc.DetachedInstanceError: Instance <MyView at 0x7f1e20237790> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3)

I suspect that the problem is somehow related to concurrency: if I set waitress to one thread, everything works fine.

Is there any way to enforce loading of all data before passing to jinja ?

0

There are 0 best solutions below