Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b17ee27798
Fetching contributors…

Cannot retrieve contributors at this time

100 lines (76 sloc) 3.538 kb
Twisted MySQL Protocol implementation v0.4
With thanks to _habnabit for the intial code (which did the vast majority of
the heavy lifting) and his qbuf library, and Dev0n for DATETIME and
affected_rows support on the result of a runOperation.
* qbuf (a Python C-extension)
* Connects lazily to MySQL and disconnects if the connection is left
idle longer than idle_timeout
* Reconnects on errors when there are pending queries (including
user-configurable errors with temporary_error_strings=[...] in the
* Now has unit tests
* Support escaping arguments with '%s' style syntax
* Actually use the database=foo argument passed into the constructor
(need to set CLIENT_* flag)
* Code comments, and a lot of tidying up
* Fixed insidious bug relating to disconnection which resulted
in corrupt/duplicated results getting sent on the wrong deferreds
* MySQL DATETIME fields now show up correctly (XXXX-XX-XX XX:XX:XX).
* When a runOperation is called, the deferred now returns the a dictionary
with the following:
- {'insert_id': int, 'affected_rows': int, 'message': str,
'warning_count': int, 'server_status': int}
* Transaction support
* Stored queries
* Connection pool
* DBAPI support - see
* Test against more than just the version of MySQL 5.1 which happened
to be installed on my ThinkPad :-)
import sys
from twisted.internet import reactor, defer
from twisted.python import log
from txmysql import client
def example():
conn = client.MySQLConnection('', 'root', secrets.MYSQL_ROOT_PASS,
idle_timeout=120, connect_timeout=30)
# This gets remembered and re-run if the connection needs reconnection
d = conn.selectDb("foo")
def selectedDb(ignored):
return conn.runOperation("insert into bar set baz='bash'")
def doneInsert(ignored):
return conn.runQuery("select * from bar")
def gotResult(data):
print repr(data)
def handleFailure(reason):
# reason can be a MySQLError with message, errno, sqlstate, query
print reason
return d
if __name__ == "__main__":
txMySQL is used in production by Hybrid Web Cluster, which is an n-redundant
web cluster with no single point of failure, which uses ZFS replication to
eliminate shared storage in loosely coupled, geographically distributed web
hosting environments. Entire data centres can fail and everything carries on
running, with data loss and downtime measured in seconds, not hours or days,
and stress caused by hardware or network connectivity failures a thing of the
It runs on dedicated hardware and/or cloud infrastructure, enabling hybrid
setups where you use dedicated hardware for performance and cloud
infrastructure for ease-of-provisioning, all within the same cluster.
Check it out at If you are involved with
hosting websites or know anyone who is, please spread the word :-)
If you just want web hosting for your own site, we offer rock solid hosting
based on the aforementioned technology at
Email luke [at] hybrid-logic [dot] co [dot] uk
Jump to Line
Something went wrong with that request. Please try again.