检查特定多进程进程对象是否正在运行并通过单击模板中的按钮来停止所述进程?

问题描述 投票:0回答:1

我可以开始一个过程 并将命令行输出流式传输到模板,直到它结束 并使用此代码使用flask和jinja运行进程时呈现模板:

@app.route('/tasks/<task>', methods=['GET', 'POST'])
def tasks(task):
    running_task = multiprocessing.current_process()
    if request.method == 'POST':
        if task == 'training':
            running_task = train_task(create_path(app.config['ARCHITECTURES_DIRECTORY'],
                                                   get('architecture_name') + '.json'),
                                       create_path(app.config['DATASET_DIRECTORY'],
                                                   get('dataset_name')),
                                       int(get('desired_image_size')),
                                       int(get('num_epochs')),
                                       int(get('checkpoint_epochs')),
                                       int(get('batch_size')),
                                       'charsets/chars.txt',
                                       float(get('learning_rate')),
                                       get('optimizer'),
                                       getlist('metrics'),
                                       get('loss'))
        flash(task + " has started.")
    return render_template('tasks.html', task=running_task)

def train_task(architecture_config_file,
               dataset_dir,
               desired_image_size,
               num_epochs,
               checkpoint_epochs,
               batch_size,
               charset_file,
               learning_rate,
               optimizer,
               metrics,
               loss):
    task = Process(target=train_model,
                         args=(
                             architecture_config_file,
                             dataset_dir,
                             learning_rate,
                             metrics,
                             loss,
                             optimizer,
                             desired_image_size,
                             charset_file,
                             ' ',
                             num_epochs,
                             batch_size,
                             checkpoint_epochs
                         ))
    task.start()
    return task

我怎样才能做到以下几点:

  1. 知道进程是否正在运行。
  2. 如果我不想让它完成,请终止该过程。
python flask subprocess
1个回答
0
投票

我设法通过在这个正在运行的进程模板中公开当前正在运行的进程的名称来解决问题。

{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block body %}
    <h3>Running Tasks</h3>
    <ul class="collection black-text">
    {% if not task %}
        <li class="collection-item">No task is running.</li>
    {% else %}
        <li class="collection-item row">
            <ul>
                <li class="col s3">{{ task }}</li>
                <li class="col s3">
                    <form method="post" action="{{ url_for('terminate', task=task) }}">
                        <button type="submit" class="waves-effect waves-light btn red">Terminate</button>
                    </form>
                </li>
            </ul>
        </li>
    {% endif %}
    </ul>
{% endblock %}
{% block extra_script %}
    <script type="text/javascript" src="{{ url_for('static', filename='js/toast.js') }}"></script>
    <script>
        toast({{ get_flashed_messages()|tojson }})
    </script>
{% endblock %}

以下是终止该过程的部分:

@app.route('/terminate/<task>', methods=['POST'])
def terminate(task):
    was_terminated_manually = False
    for running_task in multiprocessing.active_children():
        if running_task.name == task:
            running_task.terminate()
            running_task.join()
            was_terminated_manually = True
            flash(running_task.name + " is terminated")
            break
    if not was_terminated_manually:
        flash(task + " was already terminated.")
    return redirect(url_for('tasks', task='view'))
© www.soinside.com 2019 - 2024. All rights reserved.