I am quite new to twisted and I really need one thing - to run arbitrary number of functions (starting at the same tame), collect the results from all of them and do some processing.
Here is what I have:
from twisted.internet import defer
import time
# slow computing query
def process_data(num, data):
time.sleep(5)
array = []
# mock the results obtained from processed data
for i in range(0, 5):
array.append(num)
return array
def process_results(arrays):
# this should collect return arrays of all callbacks
print arrays
data = []
callbacks_refs = []
for i in range(0, 5):
d=defer.Deferred()
d.addCallback(process_data)
callbacks_refs.append(d)
callbacks = defer.DeferredList(callbacks_refs)
callbacks.addCallback(process_results)
for i, d in enumerate(callbacks_refs):
d.callback(i, data)
I was hoping that the last for loop will start the execution of all callbacks asynchronously (like normally with Promises) and all the results will be passed to process_results function that will be executed after all of the callbacks from callbacks_refs complete, but I feel that I am terribly wrong with it.