gpt4 book ai didi

python - 如何从时钟进程 Heroku Flask 访问我的 PostgreSQL 数据库?

转载 作者:行者123 更新时间:2023-12-04 09:32:21 26 4
gpt4 key购买 nike

我有一个 custom clock process在 Heroku 中运行,每天向我的用户发送电子邮件。
问题是我需要从我的数据库访问用户,但我无权访问我的 Flask 应用程序上下文,因为它在不同的进程中运行。
过去一个小时我一直在谷歌搜索,但没有运气,所以我在这里。我尝试从 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.


基本上,您建立的每个连接都将具有一致的数据库 View 。除非你在做一些非常奇怪的事情,否则你应该没事。

关于python - 如何从时钟进程 Heroku Flask 访问我的 PostgreSQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62781674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com