Get Deferred from dbpool.runQuery instead of data with Twisted and Oracle
<p>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):</p>

<pre><code>from twisted.enterprise import adbapi
from twisted.internet import defer
import service_config

ORACLE_DSN = service_config.oracle_dsn
ORACLE_USER = service_config.oracle_user
ORACLE_PASSWORD = service_config.oracle_password
dbpool = adbapi.ConnectionPool('cx_Oracle',
    user=ORACLE_USER,
    password=ORACLE_PASSWORD,
    dsn=ORACLE_DSN, port='49161')

@defer.inlineCallbacks
def ask_db():
    data = yield dbpool.runQuery("SELECT * FROM customer")

a = ask_db()
print(a)
</code></pre>

<p>I got reactor running in other module, if that is important.
Thank you in advance.</p>

<p>UPDATE:
With help of @notorious.no got working code, returning data instead of Deferred with Python 3.5:</p>

<pre><code>@defer.inlineCallbacks
def ask_db(request):
    data = yield dbpool.runQuery(request)

    return defer.returnValue(data) 
</code></pre>

<p>You get a Deferred because you're calling an <code>inlineCallback</code> which always returns a Deferred. You're also misinterpreting what <code>yield</code> does. It doesn't actually return a value from an <code>inlinceCallback</code> it just wait's for a result. Use <code>defer.returnValue()</code> to return a value (you can use a simple <code>return</code> if you're using Python 3.4+). This is what your code should look like:</p>

<pre class="lang-py prettyprint-override"><code>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()
</code></pre>

<p>The difference between what you had previously and this answer is that a callback is added so when the <code>runQuery()</code> returns with a value, the callback is executed and <code>ask_db()</code> actually returns a value you care about.</p>

<h1>References</h1>

<ul>
<li><a href="https://notoriousno.blogspot.com/2016/08/twisted-klein-database-usage.html" rel="nofollow">Database Usage with Klein/Twisted</a></li>
</ul>

python oracle twisted deferred