def time_limited(limit_seconds, iterable):
    """
    Yield items from *iterable* until *limit_seconds* have passed.

    >>> from time import sleep
    >>> def generator():
    ...     yield 1
    ...     yield 2
    ...     sleep(0.2)
    ...     yield 3
    >>> iterable = generator()
    >>> list(time_limited(0.1, iterable))
    [1, 2]

    Note that the time is checked before each item is yielded, and iteration
    stops if  the time elapsed is greater than *limit_seconds*. If your time
    limit is 1 second, but it takes 2 seconds to generate the first item from
    the iterable, the function will run for 2 seconds and not yield anything.

    """
    if limit_seconds < 0:
        raise ValueError('limit_seconds must be positive')

    start_time = monotonic()
    for item in iterable:
        if monotonic() - start_time > limit_seconds:
            break
        yield item