django中管理程序2
升级版
from os import path
TASKS_ROOT = path.dirname(path.abspath(path.dirname(__file__)))
PYTHON_ROOT = '/usr/bin/python'
job_workspace = path.join(TASKS_ROOT,'app').replace('\\', '/')
from tempfile import mkstemp
from os import fdopen,unlink,kill
from subprocess import Popen
import signal
from django.http import HttpResponse
from django.core import serializers
import psutil
def startjob(request):
request.session.modified = True
"""Start a new long running process unless already started."""
if not request.session.has_key('running_job'):
request.session['running_job'] = {} job_name = request.GET.get('job_name')
# print request.session['running_job']
#
if job_name not in request.session['running_job']:
job_execfile = ''
if job_name == 'flower':
job_execfile = " myjob.py"
if job_name == 'worker':
job_execfile = " myjob.py" if job_execfile:
# create a temporary file to save the results
outfd,outname=mkstemp()
# request.session['jobfile']=outname
outfile=fdopen(outfd,'a+')
# print job_workspace proc=Popen((PYTHON_ROOT + job_execfile).split(),shell=False,stdout=outfile,cwd=job_workspace)
# remember pid to terminate the job later
request.session['running_job'][job_name]=proc.pid
# remember tempfile to delete the job later
request.session['running_job'][job_name+'_tmpfile']=outname return JsonResponse(request.session['running_job'], safe=False) def showjob(request):
"""Show the last result of the running job."""
# print request.session if not request.session.has_key('running_job'):
RUNNING_JOB_DIC = {}
return JsonResponse(RUNNING_JOB_DIC, safe=False)
else:
RUNNING_JOB_DIC = request.session['running_job']
return JsonResponse(RUNNING_JOB_DIC, safe=False) def rmjob(request):
"""Terminate the runining job."""
request.session.modified = True
if request.session.has_key('running_job'):
job_name = request.GET.get('job_name')
#if the job in running dict()
if request.session['running_job'].has_key(job_name):
jobpid = request.session['running_job'][job_name]
filename = request.session['running_job'][job_name + '_tmpfile']
print jobpid,filename
# if the job has finished already
if not psutil.pid_exists(jobpid):
# make sure running_job dictionary has delete
#try:
del request.session['running_job'][job_name] del request.session['running_job'][job_name + '_tmpfile']
#print request.session['running_job']
return JsonResponse(request.session['running_job'], safe=False)
try:
print jobpid,filename
kill(jobpid,signal.SIGKILL) # unix only
unlink(filename)
except OSError, e:
# probably the job has finished already
return JsonResponse({'error':'kill pid or unlink tmpfile error!'}) del request.session['running_job'][job_name]
del request.session['running_job'][job_name + '_tmpfile'] return JsonResponse(request.session['running_job'], safe=False)
前端代码:
<script src="/static/js/jquery.min.js"></script>
<script>
setInterval(function() {
$.getJSON("/showjob/", function(json){
console.log(json);
if(typeof json.flower == "undefined"){
$("#resulist tr:eq(0) td:nth-child(2)").html("stop");
}
else{
$("#resulist tr:eq(0) td:nth-child(2)").html("running");
} if(typeof json.localworker == "undefined"){
$("#resulist tr:eq(1) td:nth-child(2)").html("stop");
}
else{
$("#resulist tr:eq(1) td:nth-child(2)").html("running");
} }); }, 1000); function start_stop(myservice)
{
if (myservice === "startflower")
{
$.get("/startjob/?job_name=flower",function(data,status){
$("#resulist tr:eq(0) td:nth-child(2)").html("running"); }
);
}
if (myservice === "stopflower")
{
$.get("/rmjob/?job_name=flower",function(data,status){
$("#resulist tr:eq(0) td:nth-child(2)").html("stop"); }
);
} if (myservice === "startlocalworker")
{
$.get("/startjob/?job_name=localworker",function(data,status){
$("#resulist tr:eq(0) td:nth-child(2)").html("running"); }
);
}
if (myservice === "stoplocalworker")
{
$.get("/rmjob/?job_name=localworker",function(data,status){
$("#resulist tr:eq(0) td:nth-child(2)").html("stop"); }
);
} } </script> <table class="table table-hover"> <thead>
<tr>
<th>程序</th>
<th>状态</th>
<th>操作</th> </tr>
</thead>
<tbody id="resulist"> <tr>
<td>flower</td>
<td>stop</td>
<td>
<div class="btn-group btn-group-xs">
<button type="button" onclick="start_stop(this.name)" name="startflower" class="btn btn-default">启动</button>
<button type="button" onclick="start_stop(this.name)" name="stopflower" class="btn btn-default">停止</button>
</div>
</td> </tr> <tr>
<td>local worker </td>
<td>running</td>
<td>
<div class="btn-group btn-group-xs">
<button type="button" onclick="start_stop(this.name)" name="startlocalworker" class="btn btn-default">启动</button>
<button type="button" onclick="start_stop(this.name)" name="stoplocalworker" class="btn btn-default">停止</button>
</div>
</td> </tr> <tr>
<td>DNSLog</td>
<td>running</td>
<td>
<div class="btn-group btn-group-xs">
<button type="button" onclick="start_stop(this.name)" name="x" class="btn btn-default">启动</button>
<button type="button" onclick="start_stop(this.name)" name="x" class="btn btn-default">停止</button>
</div>
</td> </tr> </tbody>
</table>
django中管理程序2的更多相关文章
- django中管理程序1
为了解决启动关闭程序方便,在django中启动结束任务的问题. urls.py ################DJANGO start kill job####################### ...
- 异步任务队列Celery在Django中的使用
前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...
- Mysql事务探索及其在Django中的实践(二)
继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...
- Mysql事务探索及其在Django中的实践(一)
前言 很早就有想开始写博客的想法,一方面是对自己近期所学知识的一些总结.沉淀,方便以后对过去的知识进行梳理.追溯,一方面也希望能通过博客来认识更多相同技术圈的朋友.所幸近期通过了博客园的申请,那么今天 ...
- Django 中url补充以及模板继承
Django中的URL补充 默认值 在url写路由关系的时候可以传递默认参数,如下: url(r'^index/', views.index,{"name":"root& ...
- django中css问题
django中加载的css,js,图片其中js和图片可以加载出来,而css没有效果.原因如下: 这是因为你安装的某些IDE 或者其他更改了注册表导致的系统的注册表\HKEY_CLASSES_ROOT\ ...
- 在Django中进行注册用户的邮件确认
之前利用Flask写博客时(http://hbnnlove.sinaapp.com),我对注册模块的逻辑设计很简单,就是用户填写注册表单,然后提交,数据库会更新User表中的数据,字段主要有用户名,哈 ...
- django中tinymce添加图片上传功能
主要参考以下: https://pixabay.com/en/blog/posts/direct-image-uploads-in-tinymce-4-42/ http://blog.csdn.net ...
- django中migration文件是干啥的
昨天很蠢的问leader git push的时候会不会把本地的数据库文件上传上去,意思是django中那些migration文件修改之后会不会上传. 然后得知不会,因为所有的数据库都存在本机的mysq ...
随机推荐
- jmeter4.0☞如何汉化(二)
如何汉化jmeter打开jmeter,选择options_choose language_Chinese(simplified),如下图: 刚刚下载使用jmeter4.0的时候有点懵圈,英语实在是差劲 ...
- 【CodeForces】9A-Die Roll
目录 Question Solution 解法1 解法2 Question 三个人掷骰子,前两个人的得分分别是Y和W,问第三个人胜利的概率(第三个人得分不小于Y.W)?结果输出格式为\(A/B\),如 ...
- 装机、UEFI双系统安装
装机设置 设置默认中图标显示查看-选项-查看-应用到文件夹 控制面板-语言-管理输入法 word-选项-取消输入法设置处于活动状态word-字体-设置默认值 高DPI的显示屏,需要使用125%的缩放, ...
- java正则表达式2 -- 匹配、切割、查找
import java.util.Arrays; /* 正则表达式的作用: 1 匹配 2 切割 3 替换 * */ public class Demo1 { public static void ma ...
- zufe 蓝桥选拔
https://zufeoj.com/contest.php?cid=1483 问题 A: A 代码: #include <bits/stdc++.h> using namespace s ...
- TCP/IP Note1
TCP/IP(Transmission Control Protocol / Internet Protocol)是用于Internet的通信协议. 计算机通信协议:是指对那些计算机必须遵守以便彼此通 ...
- 【转】64位ORACLE客户端上plsql无法识别ORACLE_HOME解决方案
转自:http://www.2cto.com/database/201503/386267.html 中文显示问号 转自:http://zhidao.baidu.com/link?url=qJDmsa ...
- BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...
- [Leetcode] count and say 计数和说
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- NOI2018 D1T1 [NOI2018]归程 解题报告
P4768 [NOI2018]归程 题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 \(n\) 个节点.\(m\) 条边的无向连通图(节点的编号从 \ ...