def anytop(win, istream=sys.stdin, n=None):
    "Visualize the incoming lines by their distribution."
    if sys.version_info.major == 2:
        istream = codecs.getreader('utf8')(istream)
    common.init_win(win)
    if n:
        accumulator = accumulate.WindowAccumulator(n)
    else:
        accumulator = accumulate.Accumulator()
    lock = threading.Lock()
    logging.debug('Starting UI thread')
    ui = AnyTopUI(win, lock, accumulator)
    ui.start()

    try:
        for line in common.robust_line_iter(istream):
            if ui.error:
                return ui.error

            logging.debug('INPUT: requesting lock')
            lock.acquire()
            logging.debug('INPUT: lock acquired')
            accumulator.consume(line)
            lock.release()
            logging.debug('INPUT: lock released')

        logging.debug('INPUT: finished')

        # wait for CTRL-C
        # XXX we should display that the input was exhausted
        logging.debug('INPUT: exhausted')
        while True:
            time.sleep(3600)

    except __HOLE__:
        logging.debug('INPUT: got CTRL-C')
        ui.stop()
        ui.join()

    finally:
        ui.stop()
        ui.join()

    return ui.error
dataset/ETHPy150Open larsyencken/anytop/anytop/top.py/anytop