I need to call two different methods simultaneously in order to save time and using results obtained from those methods i need to call another methods, So i came across deferToThread method in twisted.internet.threads which return a deferred object so that i can attach callback methods.
Here is my code
for key,val in result.items():
d=threads.deferToThread(myMethod1,key,val)
d1=threads.deferToThread(myMethod2,key)
d.addCallback(myMethod3)
d1.addCallback(myMethod4)
reactor.run()
In the given code, result is a dictionary. myMethod1 and myMethod2, these are the two methods which i need to run asynchronously.
As per my understanding, deferToThread will create a separate thread to execute given method for each item in a dictionary, So every time the loop iterates, It will create two different threads. That's fine. But i need to run this code after fix interval continuously. I tried to run it after fix interval but after executing for 16 to 17 times, every thing stops working. does it mean that lots of threads are running in background?? and if lots of thread are running then how to stop or how to kill those threads?