分类: Oracle
2010-11-30 18:28:26
#File: twistedTCPServer.py
#Querying database asynchronously with Twisted from twisted.web import resource, server from twisted.internet import reactor from twisted.enterprise import adbapi class BlobLoads(resource.Resource): def __init__(self, dbconn): self.dbconn = dbconn resource.Resource.__init__(self) def _getBlobs(self, txn, query): txn.execute(query) return txn.fetchall() def render_GET(self, request): query = "select id, blobdoc from blob_tab" self.dbconn.runInteraction(self._getBlobs, query).addCallback( self._writeBlobs, request).addErrback( self._exception, request) return server.NOT_DONE_YET def _writeBlobs(self, results, request): request.write(""" Writing BLOBs from the database to your disk""") for id, blobdoc in results: request.write("/tmp/picture%s.bmp " % id) blob = blobdoc.read() output = open("/tmp/picture%s.bmp" % id, 'wb') output.write(blob) output.close() request.write(""" Operation completed """) request.finish( ) def _exception(self, error, request): request.write("Error obtaining BLOBs: %s" % error.getErrorMessage()) request.write(""" Could not complete operation |