import subprocess


def cmd_runner(command, line_process_fn, print_line=True):
    """
    针对darknet的训练方式，返回打印的日志、学习率和map
    :param file: 命令
    :return:
    """
    p = subprocess.Popen([command], stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT, shell=True, bufsize=1)
    for line in iter(p.stdout.readline, ''):
        state = subprocess.Popen.poll(p)
        if state is not None:
            break  # 如果进程没有在进行中就break
        line = line.decode().rstrip()
        line_process_fn(line)
        if print_line:
            print(line)
