Pyramid 和 Cassandra 不能正常工作

Pyramid and Cassandra don't work properly

我正在使用 Pyramid (1.5.7)Waitress (0.8.9)Cassandra (2.2 .0)。似乎 Waitress 和 Cassandra 驱动程序都在使用 asyncore 并且不知何故他们踩到了对方的脚趾。这是我在 app/__init__.py 文件中的代码:

import logging.config
from .action.context import root_factory
from pyramid.config import Configurator
from cassandra.cluster import Cluster
from cassandra.query import named_tuple_factory


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application."""

    # support logging in python3
    logging.config.fileConfig(
        settings['logging.config'],
        disable_existing_loggers=False
    )
    config = Configurator(settings=settings, root_factory=root_factory)
    # Retrieves connection to Cassandra (Non SQL database)
    def get_non_sql(request):
        cluster = Cluster(['127.0.0.1'], port=9042)
        session = cluster.connect('app')
        def disconnect(request):
            cluster.shutdown()
        request.add_finished_callback(disconnect)
        return cluster.connect('app')

    #Adding scheduler configuration
    config.configure_celery(global_config['__file__'])
    config.add_request_method(get_non_sql, 'non_sql', reify=True)
    config.scan()
    return config.make_wsgi_app()

生成的错误:

2015-07-27 12:24:36,779 ERROR [waitress][cassandra_driver_event_loop] Socket error.
Traceback (most recent call last):
  File "python3.4/site-packages/waitress-0.8.9-py3.4.egg/waitress/channel.py", line 167, in handle_read
    data = self.recv(self.adj.recv_bytes)
  File "python3.4/asyncore.py", line 379, in recv
    data = self.socket.recv(buffer_size)
BlockingIOError: [Errno 35] Resource temporarily unavailable
2015-07-27 12:24:37,079 ERROR [waitress][MainThread] Unexpected exception when flushing.
  File "python3.4/site-packages/waitress-0.8.9-py3.4.egg/waitress/server.py", line 154, in run
    use_poll=self.adj.asyncore_use_poll,
  File "python3.4/asyncore.py", line 208, in loop
    poll_fun(timeout, map)
  File "python3.4/asyncore.py", line 145, in poll
    r, w, e = select.select(r, w, e, timeout)
OSError: [Errno 9] Bad file descriptor
2015-07-27 12:33:32,649 DEBUG [cassandra.io.asyncorereactor][cassandra_driver_event_loop] Asyncore event loop ended

有人知道这个问题的解决方法吗?

Waitresscassandra-driver 使用同一个套接字。我已经删除了waitress的0.8.9版本并安装了最新的开发版本(master分支)

pip uninstall waitress
pip install git+git://github.com/Pylons/waitress@master

asyncore 套接字在此版本中已更改,因此可以完美运行。 尽管如此,我还是建议改用 libev,它似乎比 asyncore 有更好的性能。在 OS X 中就像安装 libev 库一样简单:

brew install libev

执行此操作后,立即安装 cassandra-driver(它将自动检测 libev 库)

pip install cassandra-driver

然后只需使用 LibevConnection class:

from cassandra.io.libevreactor import LibevConnection 
from cassandra.cluster import Cluster 


cluster = Cluster() 
cluster.connection_class = LibevConnection 
session = cluster.connect()