Skip to content

g not persisting #4457

@sjordahl

Description

@sjordahl

I've built a flask app following the official tutorial and am not seeing persistence with respect to the g object. I'm attempting to persist a single Postgres connection in g.db, but the connection is being recreated on every request. All roads that I've traveled in this regard suggest that I'm using the object per design.

Here's my db.py file:

import psycopg2
from flask import g

from os import environ as env


def get_db():
    print("g: ", g.__dict__)
    if 'db' not in g:
        if env['DB_CONFIG'] == 'local':
            print("Database: using standalone local database")
            g.db = psycopg2.connect(
                                user="postgres",
                                password="xxxxxxxx",
                                host="www.xxx.yyy.zzz",
                                port="5432",
                                database="ab")
            ...
    print("Database: returning g.db")
    print("g: ", g.__dict__)
    return g.db


def close_db(e=None):
    db = g.pop('db', None)

    if db is not None:
        db.close()


def init_app(app):
    app.teardown_appcontext(close_db)

As you'll notice, I've added some debug print statements to determine what's going on, and I've reduced the choices for DB_CONFIG as they are irrelevant. This particular function is being called via Javascript's fetch function rather than through browser navigation (which shouldn't matter AFAIK). When using this code, I get the following output each time I call the check view:

192.168.X.Y - - [18/Feb/2022 16:02:18] "POST /check HTTP/1.1" 200 -
g:  {}
Database: using standalone local database
Database: returning g.db
g:  {'db': <connection object at 0x7fcd4a583540; dsn: '
g:  {'db': <connection object at 0x7fd70d9e7540; dsn: 'user=postgres password=xxx dbname=ab host=www.xxx.yyy.zzz port=5432', closed: 0>}
192.168.X.Y - - [18/Feb/2022 16:02:22] "POST /check HTTP/1.1" 200 -
g:  {}
Database: using standalone local database
Database: returning g.db
g:  {'db': <connection object at 0x7fcd4a583540; dsn: '
g:  {'db': <connection object at 0x7fd70d9e7540; dsn: 'user=postgres password=xxx dbname=ab host=www.xxx.yyy.zzz port=5432', closed: 0>}
192.168.X.Y - - [18/Feb/2022 16:02:26] "POST /check HTTP/1.1" 200 -
g:  {}
Database: using standalone local database
Database: returning g.db
g:  {'db': <connection object at 0x7fcd4a583540; dsn: '
g:  {'db': <connection object at 0x7fd70d9e7540; dsn: 'user=postgres password=xxx dbname=ab host=www.xxx.yyy.zzz port=5432', closed: 0>}
192.168.X.Y - - [18/Feb/2022 16:02:29] "POST /check HTTP/1.1" 200 -
g:  {}
Database: using standalone local database
Database: returning g.db
g:  {'db': <connection object at 0x7fcd4a583540; dsn: '
g:  {'db': <connection object at 0x7fd70d9e7540; dsn: 'user=postgres password=xxx dbname=ab host=www.xxx.yyy.zzz port=5432', closed: 0>}
192.168.X.Y - - [18/Feb/2022 16:02:32] "POST /check HTTP/1.1" 200 -
g:  {}
Database: using standalone local database
Database: returning g.db
g:  {'db': <connection object at 0x7fcd4a583540; dsn: '
g:  {'db': <connection object at 0x7fd70d9e7540; dsn: 'user=postgres password=xxx dbname=ab host=www.xxx.yyy.zzz port=5432', closed: 0>}
192.168.X.Y - - [18/Feb/2022 16:02:35] "POST /check HTTP/1.1" 200 -

So according to this output it appears that g is not persisting. I've thought about the potential thread locality of g, but I can run the code as many times as I want, greatly exceeding the number of workers, and still get the same output.

Any advice is greatly appreciated.

Flask ver: 2.0.3
Python ver: 3.9.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions