How to use deferLater in a for loop

It does not have to be in a for loop, but how would you process the data, put the list of data into an animated asynchronous "sleep". Essentially, I would like to do something like this that clearly doesn't work. It goes right through the loop without sleep. Should deferLater be used only for a method? If so, how could I achieve the same results by twisting, so there is a β€œdream” between each record to save the database?

@defer.inlineCallbacks def queryDatabase(self, kwargs): #Just testing deferLater for x in xrange(10000): yield txmongo.my_db.test_collection.save({"something":x * time.time()},safe=True) d = deferLater(reactor, 5, lambda: none) print str(datetime.datetime.now()) + ' ==> ' + str(x) 
+4
source share
1 answer

It never works, you just need to write down the problem, and I find the answer. This seems to work for me:

 @defer.inlineCallbacks def query(self, kwargs): # insert some test data for x in xrange(10000): print str(datetime.datetime.now()) + '===>' + str(x) yield deferLater(reactor, 5, lambda: none) @defer.inlineCallbacks def makeRequest(self, kwargs): g = yield self.query(kwargs) 
+3
source

Source: https://habr.com/ru/post/1439280/


All Articles