What you need to do is use a DeferredList
instead of inlineCallbacks
. Basically you provide a list of deferreds and after each one completes, a final callback with the results of all the deferreds is executed.
import treq
from twisted.internet import defer, reactor
def query(text):
get = treq.get('http://google.com')
get.addCallback(treq.content)
return get
output1 = query('one')
output2 = query('two')
final = defer.DeferredList([output1, output2]) # wait for both queries to finish
final.addCallback(print) # print the results from all the queries in the list
reactor.run()
Each query()
function will execute requests concurrently then return a Deferred
. This happens almost immediately, so basically output1
and output2
are executing at the same time. Then you append the deferreds (ie. output1
and output2
) inside a list
and pass it in DeferredList
, which itself returns a Deferred
. Finally, you add a callback to the DeferredList
to do something with the results (in this case I just print them). This is all done without the use of threads, which is the best part in my opinion! Hope this makes sense and please comment if it doesn't.
PS
If you need further help with Klein, I'm working on revamping the documentation here https://github.com/notoriousno/klein-basics (hopefully I'll make a blog post one of these days). Please take a look at some of the docs (the files with .rst
). My shameless plug is now concluded :D