如何从时钟进程 Heroku Flask 访问我的 PostgreSQL 数据库?
How Do I Access My PostgreSQL Database From a Clock Process Heroku Flask?
我在 Heroku 中有一个 custom clock process 运行 每天向我的用户发送电子邮件。
问题是我需要从我的数据库访问用户,但我无法访问我的 Flask 应用程序上下文,因为它 运行 在不同的进程中。
过去一个小时我一直在谷歌上搜索,但没有成功,所以我来了。我尝试从烧瓶中导入 current_app 但它给了我这个:
Job "check_if_users_did_devotions (trigger: cron[minute='*'], next run at: 2020-07-07 12:49:00 CDT)" raised an exception
Traceback (most recent call last):
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "C:\Users\smith\PycharmProjects\MyProject\MyProject\Tasks.py", line 11, in send_email
print(current_app)
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\werkzeug\local.py", line 366, in <lambda>
__str__ = lambda x: str(x._get_current_object())
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
return self.__local()
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\flask\globals.py", line 52, in _find_app
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
Process finished with exit code -1
我想我可能需要在时钟进程中单独连接到数据库,但我不知道这是否会导致两个单独连接到同一数据库的问题。任何帮助将不胜感激!
我所知道的所有现代客户端-服务器数据库都有处理多个连接的策略。一个很常见的策略,Multiversion Concurrency Control (MVCC), is used by PostgreSQL和许多其他数据库:
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.
基本上,您建立的每个连接都将具有一致的数据库视图。除非你做的事情真的很奇怪,否则你应该没问题。
您收到此错误是因为计划的函数取决于应用程序上下文,但 运行 处于与您的应用程序不同的进程中。
您可以创建一个单独的应用程序上下文并将其推送到您的时钟进程。
在您的 clock.py
脚本中,您需要创建一个应用程序,并将其传递给任何需要应用程序上下文才能运行的函数。
然后,将传递的应用程序的上下文推送到您的函数中。
# clock.py
app = create_app()
def foo(app):
with app.app_context():
# ... work that requires context
scheduler.add_job(tester, 'cron', [app], hour=12)
我在 Heroku 中有一个 custom clock process 运行 每天向我的用户发送电子邮件。
问题是我需要从我的数据库访问用户,但我无法访问我的 Flask 应用程序上下文,因为它 运行 在不同的进程中。
过去一个小时我一直在谷歌上搜索,但没有成功,所以我来了。我尝试从烧瓶中导入 current_app 但它给了我这个:
Job "check_if_users_did_devotions (trigger: cron[minute='*'], next run at: 2020-07-07 12:49:00 CDT)" raised an exception
Traceback (most recent call last):
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "C:\Users\smith\PycharmProjects\MyProject\MyProject\Tasks.py", line 11, in send_email
print(current_app)
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\werkzeug\local.py", line 366, in <lambda>
__str__ = lambda x: str(x._get_current_object())
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
return self.__local()
File "C:\Users\smith\PycharmProjects\MyProject\venv\lib\site-packages\flask\globals.py", line 52, in _find_app
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
Process finished with exit code -1
我想我可能需要在时钟进程中单独连接到数据库,但我不知道这是否会导致两个单独连接到同一数据库的问题。任何帮助将不胜感激!
我所知道的所有现代客户端-服务器数据库都有处理多个连接的策略。一个很常见的策略,Multiversion Concurrency Control (MVCC), is used by PostgreSQL和许多其他数据库:
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.
基本上,您建立的每个连接都将具有一致的数据库视图。除非你做的事情真的很奇怪,否则你应该没问题。
您收到此错误是因为计划的函数取决于应用程序上下文,但 运行 处于与您的应用程序不同的进程中。
您可以创建一个单独的应用程序上下文并将其推送到您的时钟进程。
在您的 clock.py
脚本中,您需要创建一个应用程序,并将其传递给任何需要应用程序上下文才能运行的函数。
然后,将传递的应用程序的上下文推送到您的函数中。
# clock.py
app = create_app()
def foo(app):
with app.app_context():
# ... work that requires context
scheduler.add_job(tester, 'cron', [app], hour=12)