python-cx_Oracle icon indicating copy to clipboard operation
python-cx_Oracle copied to clipboard

how can i combine cx_Oracle and twisted?

Open mangoIngithub opened this issue 8 years ago • 2 comments

I follow http://www.oracle.com/technetwork/articles/embedded/vasiliev-python-concurrency-087536.html the oracle

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("""
        <html>
        <head><title>BLOBs manipulating</title></head>
        <body>
          <h2>Writing BLOBs from the database to your disk</h2>
         """)
        for id, blobdoc in results:
          request.write("<i>/tmp/picture%s.bmp</i><br/>" % id)
          blob = blobdoc.read()
          output = open("/tmp/picture%s.bmp" % id, 'wb')
          output.write(blob)
          output.close()
   
        request.write("""
        <p>Operation completed</p>
        </body>
        </html>
        """)
        request.finish( )
    def _exception(self, error, request):
        request.write("Error obtaining BLOBs: %s" % error.getErrorMessage())
        request.write("""
        <p>Could not complete operation</p>
        </body>
        </html>
        """)
        request.finish( )

class SiteResource(resource.Resource):
    def __init__(self, dbconn):
        resource.Resource.__init__(self)
        self.putChild('', BlobLoads(dbconn))

if __name__ == "__main__":
    dbconn = adbapi.ConnectionPool('cx_Oracle', user='usr', password ='pswd', dsn='127.0.0.1/XE')
    site = server.Site(SiteResource(dbconn))
    print "Listening on port 8000"
    reactor.listenTCP(8000, site)
    reactor.run()

but i can a error

File "E:\Develop\soft\PyCharm 2018.1.4\helpers\pydev_pydevd_bundle\pydevd_resolver.py", line 201, in resolve return dict[key] KeyError: 'txn' the txn dont exist

mangoIngithub avatar Jul 06 '18 06:07 mangoIngithub

I am not very familiar with twisted so my attempt to use your code failed. Running the Python code succeeds just fine....but attempting to access the site yields 404. Can you provide a bit more detail on what is required to make this "work"?

The error you are specifying has nothing to do with cx_Oracle either. Is that the full callstack? What makes you think this is an issue with cx_Oracle when the error mentions pydev? I think we need more information before we can hope to help you out further.

anthony-tuininga avatar Jul 06 '18 17:07 anthony-tuininga

Closing due to lack of activity.

anthony-tuininga avatar Jul 24 '18 02:07 anthony-tuininga