def forkprint(iterator, *filenames, bufferlength=100, sep='\n'):
    """Given an iterator returning (index, line) and N filenames, prints
    the line to the index'th filename memory-efficiently.
    
    Returns a tuple of the number of printing operations.
    """
    
    operations = [0]*len(filenames)
    
    buffers = list()
    for fh in filenames:
        buffers.append(list())
    
    for index, thing in iterator:
        buffers[index].append(str(thing))
        
        if len(buffers[index]) == bufferlength:
            with open(filenames[index], 'a') as file:
                print(sep.join(buffers[index]), file=file)
                
            operations[index] += bufferlength
            buffers[index].clear()
            
    for index, (buffer, filename) in enumerate(zip(buffers, filenames)):
        if not buffer:
            continue
            
        with open(filename, 'a') as file:
            print(sep.join(buffer), file=file)
            
        operations[index] += len(buffer)
        buffer.clear()
        
    return tuple(operations)