I have a for loop that should update a pandas data frame from a postgres table that is updated by another thread every 5 seconds.
if I run the code without the for loop I get what I want which is just the latest update time. However, if I run the code by using the for loop the results do not update and remain stuck on the first risults.
Why is this happening and how can I fix the problem?
metadata = MetaData(bind=None)
table = Table(
'datastore',
metadata,
autoload=True,
autoload_with=engine
)
stmt = select([
table.columns.date,
table.columns.open,
table.columns.high,
table.columns.low,
table.columns.close
]).where(and_(table.columns.date_ == datetime.today().strftime('%Y-%m-%d') and table.columns.close != 0))
#]).where(and_(table.columns.date_ == '2023-01-12' and table.columns.close != 0))
connection = engine.connect()
for x in range(1000000):
data_from_db = pd.DataFrame(connection.execute(stmt).fetchall())
data_from_db = data_from_db[data_from_db['close'] != 0]
print(data_from_db.date.iloc[-1])
time.sleep(5)
I'm also trying the psycopg2 library and the problem is always there:
for x in range(1000000):
conn = psycopg2.connect(
host='localhost',
database='ABC',
user='postgres',
password='*******')
cur = conn.cursor()
cur.execute("select max(date) from public.datastore")
y = cur.fetchall()
print(y)
time.sleep(5)