How Do I Access My PostgreSQL Database From a Clock Process Heroku Flask? How Do I Access My PostgreSQL Database From a Clock Process Heroku Flask? heroku heroku

How Do I Access My PostgreSQL Database From a Clock Process Heroku Flask?


All modern client-server databases that I'm aware of have strategies for handling multiple connections. A very common strategy, Multiversion Concurrency Control (MVCC), is used by PostgreSQL and many other databases:

When an MVCC database needs to update a piece of data, it will not overwrite the original data item with new data, but instead creates a newer version of the data item. Thus there are multiple versions stored. The version that each transaction sees depends on the isolation level implemented. The most common isolation level implemented with MVCC is snapshot isolation. With snapshot isolation, a transaction observes a state of the data as when the transaction started.

MVCC provides point-in-time consistent views. Read transactions under MVCC typically use a timestamp or transaction ID to determine what state of the DB to read, and read these versions of the data. Read and write transactions are thus isolated from each other without any need for locking. However, despite locks being unnecessary, they are used by some MVCC databases such as Oracle. Writes create a newer version, while concurrent reads access an older version.

Basically, each connection you make will have a consistent view of the database. Unless you're doing something really weird, you should be fine.


You are getting this error because the scheduled function depends on an application context, yet is running in a separate process from your application.

You can create and push a separate application context to your clock processes.

Within your clock.py script, you'll want to create an app, and pass it to any functions that require an application context to function.

Then, push the context from the passed app within your function.

# clock.pyapp = create_app()def foo(app):    with app.app_context():        # ... work that requires contextscheduler.add_job(tester, 'cron', [app], hour=12)