【问题标题】:psycopg2: cur.fetchone() returning previous request resultpsycopg2: cur.fetchone() 返回上一个请求结果
【发布时间】:2020-04-25 01:16:43
【问题描述】:

当对同一个游标进行多个查询时,我会出现这种奇怪的行为:
上一个请求是 SELECT * FROM template WHERE id=10,结果是 1 行,使用 .fetchone()
那么:

print(cnx.cur.query)
# output OK: b"SELECT * FROM template WHERE id=10"
print(cnx.cur.statusmessage)
# output OK: SELECT 1
cnx.cur.execute("SELECT * FROM msg WHERE id=%s ORDER BY ts DESC LIMIT 1", [345])
print(cnx.cur.query)
# output OK: b"SELECT * FROM msg WHERE id=345 ORDER BY ts DESC LIMIT 1"
print(cnx.cur.statusmessage, cnx.cur.rowcount)
# output OK: 1 1
row = cnx.cur.fetchone()
print(row)
# output KO: prints result from previous query, ie table template

psycopg2 pg 连接始终保持打开数周,使用相同的游标(一个 RealDictCursor,一个 DictCursor,取决于我的查询)。
但这是我多年来第一次遇到这样的碰撞,有什么见解吗?

编辑:
在 .execute() 之前创建一个新游标 (DictCursor) - 和相同的 cnx- 并在 .fetchone() 之后为每个请求关闭它并没有太大变化,仍然不时有最后一个请求结果(来自模板的行而不是来自 msg )

【问题讨论】:

    标签: postgresql psycopg2 uwsgi


    【解决方案1】:

    我从 psycopg2 2.7.1 升级到 2.8.4
    然后通过在 uwsgi 配置中添加lazy-apps = True 解决了这个SSL error。
    到目前为止,光标不再发生碰撞

    简而言之,uwsgi 是分叉的,共享相同的 pg cnx,而 psycopg2 连接在此级别不是线程安全的。似乎 psycopg2 现在会引发与此相关的异常(SSL 错误)。

    【讨论】:

    • @MauriceMeyer 我试过了,不得不等待 2 天。我会在 4 小时内完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2016-07-22
    相关资源
    最近更新 更多