I am trying to get some data from Oracle, using Twisted and runQuery and keep getting Deferred instead of actual data.
How can this be solved?
Some code (I excluded some unnecessary parts, but the idea should be clear):
from twisted.enterprise import adbapi
from twisted.internet import defer
ORACLE_DSN = service_config.oracle_dsn
ORACLE_USER = service_config.oracle_user
ORACLE_PASSWORD = service_config.oracle_password
dbpool = adbapi.ConnectionPool('cx_Oracle',
data = yield dbpool.runQuery("SELECT * FROM customer")
a = ask_db()
data = yield dbpool.runQuery(request)
You get a Deferred because you're calling an
inlineCallback which always returns a Deferred. You're also misinterpreting what
yield does. It doesn't actually return a value from an
inlinceCallback it just wait's for a result. Use
defer.returnValue() to return a value (you can use a simple
return if you're using Python 3.4+). This is what your code should look like:
from __future__ import print_function #... @defer.inlineCallbacks def ask_db(): data = yield dbpool.runQuery("SELECT * FROM customer") defer.returnValue(data) # actually return a value a = ask_db() # this returns a Deferred so add callbacks! a.addCallback(print) # add a useful callback to processes query list reactor.run()
The difference between what you had previously and this answer is that a callback is added so when the
runQuery() returns with a value, the callback is executed and
ask_db() actually returns a value you care about.