I am trying to define something like the following generator in python 2.6:
def getGenerator(self, keyIterator):
def bufferFullCallback(list_of_keys):
some_values = api_call(list_of_keys)
for value in some_values:
yield value # I want this value to be yielded by getGenerator
for key in keyIterator:
with MyBuffer(self.buffer_size, bufferFullCallback) as buf:
buf.addToBuffer(key)
In here, I get a callback when the buffer is full, wherein I make some API call to get a bunch of values corresponding to the keys. Now, I need 'getGenerator' to yield these values that it receives in the callback function. However, if I yield values in the callback, that callback essentially turns into a generator; this is clearly the wrong approach. Could you please suggest the correct way to yield values here from within a callback, or even perhaps a different refactoring that could help?
Something like this:
def getGenerator(self, keyIterator):
results = []
def bufferFullCallback(list_of_keys):
results.extend(api_call(list_of_keys))
for key in keyIterator:
with MyBuffer(self.buffer_size, bufferFullCallback) as buf:
buf.addToBuffer(key)
for value in results:
yield value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.