- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们的系统在 Ubuntu、python 3.4、postgres 9.4.x 和 psycopg2 上运行。
我们(将来会)使用模式在 dev
、test
和 prod
环境之间进行拆分。我创建了一个方便的方法来创建与我们数据库的连接。它使用 json 连接配置文件来创建连接字符串。我想将连接配置为使用返回的连接对所有后续查询使用特定模式。我不希望我的查询具有硬编码架构,因为我们应该能够根据我们是处于开发、测试还是生产阶段/环境,轻松地在它们之间切换。
目前便捷的方法如下所示:
def connect(conn_config_file = 'Commons/config/conn_commons.json'):
with open(conn_config_file) as config_file:
conn_config = json.load(config_file)
conn = psycopg2.connect(
"dbname='" + conn_config['dbname'] + "' " +
"user='" + conn_config['user'] + "' " +
"host='" + conn_config['host'] + "' " +
"password='" + conn_config['password'] + "' " +
"port=" + conn_config['port'] + " "
)
cur = conn.cursor()
cur.execute("SET search_path TO " + conn_config['schema'])
return conn
只要您给它时间执行设置的 search_path
查询,它就可以正常工作。不幸的是,如果我执行以下查询的速度太快,则会在未设置 search_path
的情况下发生竞争条件。我试图通过在 return conn
之前执行 conn.commit()
来强制执行,但是,这会将 search_path
重置为默认模式 postgres
这样它就不会使用,比如说,prod
。在数据库或应用程序层的建议是更可取的,但是,我知道我们也可以在操作系统级别解决这个问题,也欢迎在这个方向上提出任何建议。
示例 json 配置文件如下所示:
{
"dbname": "thedatabase",
"user": "theuser",
"host": "localhost",
"password": "theusers_secret_password",
"port": "6432",
"schema": "prod"
}
非常感谢任何建议。
最佳答案
我认为更优雅的解决方案是在 connect()
的 options
参数中设置 search_path
,如下所示:
def connect(conn_config_file = 'Commons/config/conn_commons.json'):
with open(conn_config_file) as config_file:
conn_config = json.load(config_file)
schema = conn_config['schema']
conn = psycopg2.connect(
dbname=conn_config['dbname'],
user=conn_config['user'],
host=conn_config['host'],
password=conn_config['password'],
port=conn_config['port'],
options=f'-c search_path={schema}',
)
return conn
当然,您可以使用“选项”作为连接字符串的一部分。但是使用关键字参数可以避免字符串连接的所有麻烦。
我在这个 psycopg2 feature request 中找到了这个解决方案.至于“options”参数本身,提到了here。 .
关于python - 为 psycopg2 : Getting race condition when setting search_path 中的所有连接查询设置架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32812463/
我经常发现这些术语被用在并发编程的上下文中。它们是相同的还是不同的? 最佳答案 不,它们不是同一件事。它们不是彼此的子集。它们也不是彼此的必要条件,也不是充分条件。 数据竞争的定义非常明确,因此,它的
我在测试我的项目时遇到了 DATA RACE 警告,想知道是否有人愿意帮助我破译这个问题。我过去从未尝试过测试 go 例程,我发现很难全神贯注于数据竞赛。 我在描述中提供了指向未解决问题的链接,并在问
起初,我知道代码有一些竞争条件,所以我使用“go build -race”命令来检查它,我想看看结果如何显示,当我第一次运行时,它显示了第一个结果如下,然后再次运行显示第二个,它有两个不同的结果,我不
我在考虑我的代码中的极端情况,我不知道如何在检查文件是否存在时避免问题,如果不存在,则创建一个具有该文件名的文件。代码大致如下所示: // 1 status = stat(filename); if
我在考虑我的代码中的极端情况,我不知道如何在检查文件是否存在时避免问题,如果不存在,则创建一个具有该文件名的文件。代码大致如下所示: // 1 status = stat(filename); if
我想知道是否存在插入查询上实际发生竞争条件问题的真实案例。所以我有一个包含以下字段的“用户”表: User: iduser | idcompany | name | email 我为此表使用复合主键(
我有一段代码(一个测试运行器)应该运行代码并返回结果,我想为每个测试用例设置一个时间限制所以我使用 Promise.race 但不幸的是它不起作用 const createTestCafe = req
我遇到了 an implementation JavaScript 中的 Promise.race() 方法,它按预期工作,但对我来说意义不大。 const race = (...promises)
我的测试代码如下,使用threading,count不是5,000,000,所以出现data race,但是使用gevent,count是5,000,000,没有data race。 难道gevent
据我所知,关于promise有两个选项: promise.all() promise.race() 好的,我知道 promise.all() 是做什么的。它并行运行 Promise,如果两者都成功解析
又是我和我的BlockingQueue...我根据this article重写了它和 this question .它发送了一些项目,然后因访问冲突而崩溃。这是代码: template bool D
所以我有两个Python3.2进程需要相互通信。大多数需要交流的信息都是标准词典。命名管道似乎是可行的方法,所以我制作了一个可以在两个进程中实例化的管道类。这个类实现了一个非常基本的协议(protoc
我有一个注册页面,它接收 token 并解析它们并在参数适用时登录用户。 在我检查 token 的时间到我从数据库中删除 token 的时间之间,另一个用户可以使用相同的 token 登录。有没有办法
我在 redis 中有一个散列,其中一个字段的值为字符串化数组,每当用户注册一个事件时, 从redis中获取这个字符串化数组 后台解析,将用户的用户名添加到数组中 将数组字符串化并存储回哈希 如果两个
我知道之前有人问过这个问题,但我仍然很困惑,如果可能的话,我想在开始编程之前避免任何问题。 我计划拥有一个在任何给定时间至少有 100 名活跃用户的内部网站。用户将发布一个项目(以 0 作为其值插入到
我在面试中被问到以下问题。给定以下代码,如果方法 add 和 doAction 被多个线程调用,我们如何在打印 toString 时得到 NullPointerException ?** public
为什么标志“-race”的结果与预期的不一样?它期望相同的结果:1000000 - 带有标志“-race”但没有这个 https://gist.github.com/romanitalian/f403
我正在尝试了解如何为以下代码修复此竞争条件。 sayHello := func() { fmt.Println("Hello from goroutine") } go sayHello()
我有一堆 goroutines 在循环中做一些事情。我希望能够暂停所有这些,运行一些任意代码,然后恢复它们。我尝试这样做的方式可能不是惯用的(我希望有更好的解决方案),但我不明白为什么它不起作用。 精
考虑一个实现 open()、read()、write()、close()、unlocked_ioctl() 和 mmap() 的 linux 设备驱动程序。 现在,假设多个(或相同的)进程同时打开同一
我是一名优秀的程序员,十分优秀!