Spaces:
Running
Running
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 | |