def buffered_generator(source_gen, buffer_size=2, use_multiprocessing=False):
    r
    if buffer_size < 2:
        raise RuntimeError("Minimal buffer_ size is 2!")
    if use_multiprocessing:
        print('WARNING seems to freeze if passed in a generator')
        if False:
            pool = multiprocessing.Pool(processes=get_default_numprocs(),
                                        initializer=init_worker,
                                        maxtasksperchild=None)
            Process = pool.Process
        else:
            Process = multiprocessing.Process
        _Queue = multiprocessing.Queue
        target = _buffered_generation_process
    else:
        _Queue = queue.Queue
        Process = KillableThread
        target = _buffered_generation_thread
    buffer_ = _Queue(maxsize=buffer_size - 1)
    sentinal = StopIteration
    process = Process(
        target=target,
        args=(iter(source_gen), buffer_, sentinal)
    )
    process.daemon = True
    process.start()
    while True:
        output = buffer_.get()
        if output is sentinal:
            raise StopIteration
        yield output