Skip to content

Instantly share code, notes, and snippets.

@hagsteel
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagsteel/6113b0e0e9cd4b4c3585 to your computer and use it in GitHub Desktop.
Save hagsteel/6113b0e0e9cd4b4c3585 to your computer and use it in GitHub Desktop.
from django.db import connection
from swampdragon_auth.socketconnection import HttpDataConnection
from tornado import ioloop
class MysqlHeartbeatConnection(HttpDataConnection):
def _close_db_connection(self):
connection.close()
def on_open(self, request):
super(MysqlHeartbeatConnection, self).on_open(request)
# Change the callback time to be closer to DB timeout
iol = ioloop.IOLoop.current()
self.db_heartbeat = ioloop.PeriodicCallback(self._close_db_connection, callback_time=3000, io_loop=iol)
self.db_heartbeat.start()
def on_close(self):
super(MysqlHeartbeatConnection, self).on_close()
self._close_db_connection()
self.db_heartbeat.stop()
def on_message(self, data):
self.db_heartbeat.stop()
self.db_heartbeat.start()
super(MysqlHeartbeatConnection, self).on_message(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment