Spaces:
Running
Running
File size: 835 Bytes
c0be431 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import asyncio
async def async_zip_stream(*iterators, default_value=''):
tasks = [asyncio.create_task(iterator.__anext__()) for iterator in iterators]
done = [False] * len(iterators)
while not all(done):
results = []
for i, task in enumerate(tasks):
if done[i]:
results.append(default_value)
elif task.done():
try:
results.append(task.result())
tasks[i] = asyncio.create_task(iterators[i].__anext__())
except StopAsyncIteration:
done[i] = True
results.append(default_value)
else:
results.append(default_value)
yield tuple(results)
await asyncio.sleep(0.01) # Slight delay to allow other tasks to progress
|