Skip to content Skip to sidebar Skip to footer

How Do I Use Threads On A Generator (multiple Threads Per Item) While Keeping The Order?

I have a code that is mimicking a REST API call (see below). For every key in the item of the generator, it needs to run a REST call. So in my example, a record could be {'a': 2, '

Solution 1:

1st issue here is that your looping over keys in a record that grows...

for key in list(record):  # make a copy of the keys!

I think the 2nd issue here is that you have 3 keys and 8 threads... len(chunk) will be 3, 6, 9 ... threads is 8 - the following condition is not reached

iflen(chunk) == threads:  # try len(chunk) >= threadsyield chunk
            chunk = []

last issue is that you yield uncompleted records before all threads are finish. here is a possible fix:

defunchunk(chunk_gen):
    """Flattens a generator of Future chunks into a generator of Future results."""for chunk in chunk_gen:
        old_res = Nonefor f in chunk:
            res = f.result() # get result from Futureif old_res and res isnot old_res:
                yield old_res
            old_res = res
    if old_res:
        yield old_res

Post a Comment for "How Do I Use Threads On A Generator (multiple Threads Per Item) While Keeping The Order?"