postgresql 如何使用 psycopg 和 gevent 池连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12650048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:21:46  来源:igfitidea点击:

How can I pool connections using psycopg and gevent?

pythonpostgresqlasynchronousgeventpsycopg

提问by gone

The psycopg docs state: "Psycopg connections are not green thread safe and can't be used concurrently by different green threads. Trying to execute more than one command at time using one cursor per thread will result in an error (or a deadlock on versions before 2.4.2). Therefore, programmers are advised to either avoid sharing connections between coroutines or to use a library-friendly lock to synchronize shared connections, e.g. for pooling."

psycopg 文档指出:“Psycopg 连接不是绿色线程安全的,不能由不同的绿色线程同时使用。尝试使用每个线程一个游标一次执行多个命令将导致错误(或版本死锁) 2.4.2 之前)。因此,建议程序员避免在协程之间共享连接或使用库友好的锁来同步共享连接,例如用于池化。”

I can't find an implementation of pool that is green thread safe - are there any out there?

我找不到绿色线程安全的池实现 - 有吗?

采纳答案by Code Painters

I assume you know gevent-psycopg2module, which makes psycopggreenlet-friendly.

我假设你知道gevent-psycopg2模块,它使psycopggreenlet 友好。

Looking for connection pooling solution I've tried 2 solutions:

寻找连接池解决方案我已经尝试了 2 个解决方案:

  • SQLALchemy- it seems to work properly with monkey-patched threads and gevent-psycopg2. The QueuePoolclass uses threadingmodule internally for locking, monkey patching is thus necessary, even though gevent-psycopg2makes psycopg2green.

  • there's a psycopg2connection pooling examplein geventexamples

  • SQLALchemy- 它似乎与猴子补丁线程和gevent-psycopg2. 本QueuePool类使用threading内部模块锁定,猴子修补因此,有必要,即使gevent-psycopg2psycopg2绿色。

  • psycopg2示例中有一个连接池gevent示例

I've tried both solutions, but not at production load - so I can't say about their robustness yet.

我已经尝试了这两种解决方案,但不是在生产负载下 - 所以我还不能说它们的健壮性。

回答by piro

If you call gevent.monkey.patch_thread()you should be able to use psycopg2.pool.ThreadedConnectionPool.

如果你打电话,gevent.monkey.patch_thread()你应该可以使用psycopg2.pool.ThreadedConnectionPool.

回答by Denis Bilenko

Here's a good pool (not just example, but used in production): psycopg2_pool.py

这是一个很好的池(不仅仅是示例,还用于生产):psycopg2_pool.py