我可以启动进程并将命令行输出流式传输到模板直到它结束并在进程运行时使用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
我如何能够执行以下操作:
答案 0 :(得分: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'))