Psycopg2并发问题

发布于 2025-01-07 14:12:27 字数 720 浏览 0 评论 0原文

我正在尝试在多个线程之间共享 psycopg2 连接。正如文档中提到的,每当我在新线程中使用它时,我都会通过从共享连接创建新的游标对象来实现这一点。

def delete(conn):
    while True:
        conn.commit()

def test(conn):    
    cur = conn.cursor()
    thread.start_new_thread(delete,(conn,))
    i = 1
    while True:
        cur.execute("INSERT INTO mas(taru,s) values (2,%s)",(i,))
        print i
        i = i +1
        conn.commit()

运行后,我得到类似的输出,

1
2
...
98  
99
Traceback (most recent call last):
File "postgres_test_send.py", line 44, in <module>
   cur.execute("INSERT INTO mas(taru,s) values (2,%s)",(i,))
psycopg2.InternalError: SET TRANSACTION ISOLATION LEVEL must be called before any query

这是怎么回事?

I am trying to do share a psycopg2 connection between multiple threads. As was mentioned in the docs, I am doing that by creating new cursor objects from the shared connection, whenever I use it in a new thread.

def delete(conn):
    while True:
        conn.commit()

def test(conn):    
    cur = conn.cursor()
    thread.start_new_thread(delete,(conn,))
    i = 1
    while True:
        cur.execute("INSERT INTO mas(taru,s) values (2,%s)",(i,))
        print i
        i = i +1
        conn.commit()

After running, I get output like,

1
2
...
98  
99
Traceback (most recent call last):
File "postgres_test_send.py", line 44, in <module>
   cur.execute("INSERT INTO mas(taru,s) values (2,%s)",(i,))
psycopg2.InternalError: SET TRANSACTION ISOLATION LEVEL must be called before any query

What's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2025-01-14 14:12:27

该错误不在最新的 psycopg2 版本中:它可能已在 2.4.2 中修复。

The bug is not in the most recent psycopg2 versions: it has probably been fixed in 2.4.2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文