首页 > 解决方案 > 在服务器内运行可执行的背景程序

问题描述

我是我的大学实验室的一名实习生,从事空气质量研究,他们希望我改进他们的网站,该网站运行空气质量研究人员使用的程序之一(我认为该程序是用 RUST 编写的)。它现在的工作方式是用户将一些必要的文件上传到 webapp(用 Django btw 编写),然后 webapp 调用这个程序,该程序在服务器上运行上传的文件。问题是如果用户离开或刷新页面,后台程序就会停止。我从来没有处理过在服务器后台运行其他可执行文件的概念,所以我在理解这一切时遇到了一些问题。我遇到了 python 的“Celery”包,但它似乎很难实现,我想知道是否还有其他方法可以做到这一点。

标签: djangoweb-development-server

解决方案


您可以使用子流程来完成

template.html

<form action="{% url 'run_sh' %}" method="POST">
    {% csrf_token %}
    <button type="submit">Call</button>
</form>

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('run-sh/', views.index, name='run_sh')
]

views.py

import subprocess

def index(request):
    if request.POST:
        subprocess.call('/home/user/test.sh') 

    return render(request,'index.html',{})

test.sh

#!/bin/sh
echo 'hello'

根据对这个问题的回答


推荐阅读