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
How can I pool connections using psycopg and gevent?
提问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 psycopg
greenlet-friendly.
我假设你知道gevent-psycopg2模块,它使psycopg
greenlet 友好。
Looking for connection pooling solution I've tried 2 solutions:
寻找连接池解决方案我已经尝试了 2 个解决方案:
SQLALchemy
- it seems to work properly with monkey-patched threads andgevent-psycopg2
. TheQueuePool
class usesthreading
module internally for locking, monkey patching is thus necessary, even thoughgevent-psycopg2
makespsycopg2
green.there's a
psycopg2
connection pooling exampleingevent
examples
SQLALchemy
- 它似乎与猴子补丁线程和gevent-psycopg2
. 本QueuePool
类使用threading
内部模块锁定,猴子修补因此,有必要,即使gevent-psycopg2
让psycopg2
绿色。
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