<p>在<code>cassandra-driver</code>常见问题解答中也有类似的问题,其中WSGI应用程序不能与全局连接池一起工作:</p>
<blockquote>
<p>Depending on your application process model, it may be forking after driver Session is created. Most IO reactors do not handle this, and problems will manifest as timeouts. [Here][1]</p>
</blockquote>
<p>这至少让我正确地检查了可用的连接类:结果发现,<code>cassandra.io.twistedreactor.TwistedConnection</code>在awslambda上工作得很好。在</p>
<p>总的来说,代码是这样的:</p>
<pre><code>from cassandra.cluster import Cluster
from cassandra.io.twistedreactor import TwistedConnection
import time
SESSION = Cluster([...], connection_class=TwistedConnection).connect()
def run(event, context):
t0 = time.time()
x = list(SESSION.execute('SELECT * FROM keyspace.table')) # Ensure query actually evaluated
print('took', time.time() - t0)
</code></pre>
<p>不过,您需要在venv中安装<code>twisted</code>。在</p>
<p>我在一分钟的crontab上运行了一夜,只看到一些连接错误(一小时内最多2个),所以总体上对这个解决方案很满意。在</p>
<p>另外,我还没有测试基于<code>eventlet</code>和<code>gevent</code>的连接,因为我不能让它们猴子修补我的应用程序,而且我也不想编译<code>libev</code>来在lambda上使用。也许有人想试试。在</p>
<p>别忘了
<a href="http://datastax.github.io/python-driver/faq.html#why-do-connections-or-io-operations-timeout-in-my-wsgi-application" rel="nofollow noreferrer">http://datastax.github.io/python-driver/faq.html#why-do-connections-or-io-operations-timeout-in-my-wsgi-application</a></p>