def buffered_read(fh, lock, offsets, bytecounts, buffersize=2 ** 26):
    """Return iterator over segments read from file."""
    length = len(offsets)
    i = 0
    while i < length:
        data = []
        with lock:
            size = 0
            while size < buffersize and i < length:
                fh.seek(offsets[i])
                bytecount = bytecounts[i]
                data.append(fh.read(bytecount))
                size += bytecount
                i += 1
        for segment in data:
            yield segment
