问题描述
该psycopg文档状态:Psycopg连接不上的绿色线程安全的,不能由不同的绿色线程同时使用尝试使用每个线程的一个光标会导致错误的时间执行多个命令(或。 2.4.2之前僵局版本)。
因此,我们建议程序员或避免协同程序之间共享连接或使用图书馆友好的锁来同步共享的连接,例如为池。
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."
我找不到池的实现,是绿色的线程安全? - 是否有任何那里
I can't find an implementation of pool that is green thread safe - are there any out there?
推荐答案
我假定你知道模块,这使得 psycopg
greenlet友好。
I assume you know gevent-psycopg2 module, which makes psycopg
greenlet-friendly.
寻找连接池解决方案,我已经试过2解决方法:
Looking for connection pooling solution I've tried 2 solutions:
-
的SQLAlchemy
- 它似乎与猴子打补丁的线程正常工作,GEVENT-psycopg2的
。该类使用线程
内部锁定模块,猴子修补因此,有必要,即使GEVENT-psycopg2的
品牌psycopg2的
绿色
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.
有一个 psycopg2的
的
there's a psycopg2
connection pooling example in gevent
examples
我已经试过这两种解决方案,而不是在生产负荷 - 所以我不能说他们的鲁棒性尚未
I've tried both solutions, but not at production load - so I can't say about their robustness yet.
这篇关于我怎样才能池中使用psycopg和GEVENT连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!