Cassandra, working asynchronous

513 views
Skip to first unread message

Дмитрий Белавенцев

unread,
Nov 21, 2011, 11:31:37 AM11/21/11
to Tornado Web Server
Hi, here ( http://stackoverflow.com/questions/8213049/cassandra-or-mongodb-for-good-scaling-and-big-amount-of-queries
) is my original post in StackOverflow. I want to use Cassandra in my
project. I also use Tornado.

I want to ask - does some async Cassandra lib for tornado exists or
not? How can I communicate asynchronously with Cassandra from Tornado?

Or maybe Cassandra will not bring any difference and I should use
async lib for MongoDB (and MongoDB too)?

David King

unread,
Nov 21, 2011, 1:54:30 PM11/21/11
to python-...@googlegroups.com
Cassandra uses Thrift for its wire-protocol, so your best bet is finding an async Thrift driver

David Koblas

unread,
Nov 21, 2011, 2:23:04 PM11/21/11
to python-...@googlegroups.com, David King
We're doing exactly that, with the 2.1 release of Tornado there is
support for twisted. We've integrated telephus.pool in this way:

import tornado.platform.twisted
from telephus.pool import CassandraClusterPool
from twisted.internet import reactor

tornado.platform.twisted.install()

from twisted.internet import reactor

pool = CassandraClusterPool([HOST], keyspace='XXXX',
reactor=reactor)

pool.startService()

reactor.run() # this calls
tornado.ioloop.IOLoop.instance().start()


The one big note is that you need a git checked out version of Tornado,
since there was one bug fix we had to make to keep twisted happy.

--koblas

Peter Bengtsson

unread,
Nov 21, 2011, 7:08:35 PM11/21/11
to python-...@googlegroups.com
You could try to do it synchronously (i.e. blocking) and it might just be as fast as doing it async. 
After all, the queries are tiny and quick (hopefully) and doing it async would involve more CPU work to go back and forth in the IO loop instead of just getting it done. 
Just a thought. You might be able to get some interesting results. 

Дмитрий Белавенцев

unread,
Nov 22, 2011, 1:23:36 AM11/22/11
to Tornado Web Server
What do you think about using async telephus: will it involve more CPU
work?

Joe Bowman

unread,
Nov 22, 2011, 7:49:00 AM11/22/11
to python-...@googlegroups.com
After writing the asynchronous sessions library using mongodb and asyncmongo, I'd honestly suggest you try just using Cassandra synchronously first as well. Cassandra is really fast and you may find that it's not worth the code complexity to make it asynchronous. 

That said, there's also CQL for Cassandra now, here's a link to the python package for it - http://pypi.python.org/pypi/cql/1.0.5

You may be able to adapt that to fit your needs as well.

Дмитрий Белавенцев

unread,
Nov 22, 2011, 11:39:30 AM11/22/11
to Tornado Web Server
Thx! I now have only some quesitons about code. I have the following
code:

import sys
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.escape


import tornado.platform.twisted
from telephus.pool import CassandraClusterPool
from twisted.internet import reactor

class PlaceHandler(tornado.web.RequestHandler):
def get(self, id):
self.write('GET detected')

def post(self):
login = self.get_argument("login")
password = self.get_argument("pass")
if login == "dizpers" and password == "test":
self.write(tornado.escape.json_encode({"authed":"y"}))
else:
self.write(tornado.escape.json_encode({"authed":"n"}))


application = tornado.web.Application([
(r"/auth", PlaceHandler)
])

if __name__ == "__main__":
#tornado.platform.twisted.install() <= if don't comment this, i
receive ReactorAlreadyInstalledError
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(sys.argv[1])
pool = CassandraClusterPool(['localhost'], keyspace='Test',
reactor=reactor)
pool.startService()
reactor.run()
tornado.ioloop.IOLoop.instance().start()

You wrote that reactor.run() starts IOLoop too, but if don't use
tornado.ioloop.IOLoop.instance().start() in last line - script don't
handle POST or GET requests

On 22 ноя, 19:49, Joe Bowman <bowman.jos...@gmail.com> wrote:
> After writing the asynchronous sessions library using mongodb and
> asyncmongo, I'd honestly suggest you try just using
> Cassandra synchronously first as well. Cassandra is really fast and you may
> find that it's not worth the code complexity to make it asynchronous.
>
> That said, there's also CQL for Cassandra now, here's a link to the python

> package for it -http://pypi.python.org/pypi/cql/1.0.5

Reply all
Reply to author
Forward
0 new messages