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):
    def wrapper(self, *args, **kwargs):
            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))
        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):
    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