Database connectionsΒΆ

Cyclone supports a number of different databases, from RDBMs to NoSQL:

Here is an example of a server with both Redis and MySQL:

import MySQLdb
import functools
import cyclone.redis
from twisted.internet import defer
from twisted.python import log
from twisted.enterprise import adbapi

def dbsafe(method):
    @defer.inlineCallbacks
    @functools.wraps(method)
    def wrapper(self, *args, **kwargs):
        try:
            result = yield defer.maybeDeferred(method, self, *args, **kwargs)
        except MySQLdb.OperationalError, e:
            log.msg("MySQL error: " + str(e))
        except cyclone.redis.RedisError, e:
            log.msg("Redis error: " + str(e))
        else:
            defer.returnValue(result)
        raise web.HTTPError(503)  # Service Unavailable
    return wrapper

class DatabaseMixin(object):
    redis = cyclone.redis.lazyConnectionPool()
    mysql = adbapi.ConnectionPool("MySQLdb", db="dummy")

class MainHandler(web.RequestHandler, DatabaseMixin):
    @dbsafe
    @defer.inlineCallbacks
    def get(self):
        rs1 = yield self.mysql.runQuery("SELECT 1")
        rs2 = yield self.redis.get("foo")
        ...

Previous topic

Integration with other services

Next topic

cyclone.auth — Third-party login with OpenID and OAuth

This Page