我怎样才能池中使用psycopg和GEVENT连接? [英] How can I pool connections using psycopg and gevent?

查看:272
本文介绍了我怎样才能池中使用psycopg和GEVENT连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该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?

推荐答案

我假定你知道 GEVENT-psycopg2的模块,这使得 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:


  • 的SQLAlchem​​y - 它似乎与猴子打补丁的线程正常工作, GEVENT-psycopg2的。该 QueuePool 类使用线程内部锁定模块,猴子修补因此,有必要,即使 GEVENT-psycopg2的品牌 psycopg2的绿色

  • SQLALchemy - it seems to work properly with monkey-patched threads and gevent-psycopg2. The QueuePool class uses threading module internally for locking, monkey patching is thus necessary, even though gevent-psycopg2 makes psycopg2 green.

有一个 psycopg2的连接池 GEVENT 例子例如

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连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆