Python

Run sync Iterables as async Stream

admin by @admin ADMIN
20h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Wrap a blocking iterator so each yield happens in a worker thread — useful when integrating sync libraries (DB cursors, file iterators) into an async pipeline without rewriting them.
Python
Raw
import asyncio
from typing import AsyncIterator, Iterable, TypeVar

T = TypeVar("T")

async def aiter_sync[T](source: Iterable[T]) -> AsyncIterator[T]:
    """Pump a sync iterable through to_thread without blocking the loop."""
    it = iter(source)
    sentinel = object()
    while True:
        item = await asyncio.to_thread(next, it, sentinel)
        if item is sentinel:
            return
        yield item

async def main():
    # Stream a (blocking) file line-by-line in an async context
    with open("/etc/hosts") as f:
        async for line in aiter_sync(f):
            print(line.rstrip())

asyncio.run(main())
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.