django - 在服务器内运行可执行的背景程序
问题描述
我是我的大学实验室的一名实习生,从事空气质量研究,他们希望我改进他们的网站,该网站运行空气质量研究人员使用的程序之一(我认为该程序是用 RUST 编写的)。它现在的工作方式是用户将一些必要的文件上传到 webapp(用 Django btw 编写),然后 webapp 调用这个程序,该程序在服务器上运行上传的文件。问题是如果用户离开或刷新页面,后台程序就会停止。我从来没有处理过在服务器后台运行其他可执行文件的概念,所以我在理解这一切时遇到了一些问题。我遇到了 python 的“Celery”包,但它似乎很难实现,我想知道是否还有其他方法可以做到这一点。
解决方案
您可以使用子流程来完成
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'
根据对这个问题的回答
推荐阅读
- php - 将记录绑定到另一个实体
- react-native - console.error:"fontFamily "layar-bahtera-regular" 不是系统字体,还没有通过 Font.loadAsync 加载
- javascript - 如何将我的 json 对象插入 Google 图表行
- python - 存储要以加密形式存储的原始未加密文件的 sha256 哈希是否安全?
- wordpress - 过滤 Gutenberg 块编辑器控件
- google-chrome-devtools - puppeteer 中的性能分析
- unix - 将代码行转换为 usb 上 archlinux 的脚本(dos2unix 和 $HOME 不起作用)
- excel - 以 Excel 表格格式从 Excel 复制并粘贴到 Power Point(“保留源格式”)
- flutter - 我们如何使用 SHA256 或在 iOS 和 Android 中的 Flutter 中从相同的字符串生成相同的哈希码?
- ionic4 - 尝试使用 ngIf 显示离子标签