升级版

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的更多相关文章

  1. django中管理程序1

    为了解决启动关闭程序方便,在django中启动结束任务的问题. urls.py ################DJANGO start kill job####################### ...

  2. 异步任务队列Celery在Django中的使用

    前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...

  3. Mysql事务探索及其在Django中的实践(二)

    继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...

  4. Mysql事务探索及其在Django中的实践(一)

    前言 很早就有想开始写博客的想法,一方面是对自己近期所学知识的一些总结.沉淀,方便以后对过去的知识进行梳理.追溯,一方面也希望能通过博客来认识更多相同技术圈的朋友.所幸近期通过了博客园的申请,那么今天 ...

  5. Django 中url补充以及模板继承

    Django中的URL补充 默认值 在url写路由关系的时候可以传递默认参数,如下: url(r'^index/', views.index,{"name":"root& ...

  6. django中css问题

    django中加载的css,js,图片其中js和图片可以加载出来,而css没有效果.原因如下: 这是因为你安装的某些IDE 或者其他更改了注册表导致的系统的注册表\HKEY_CLASSES_ROOT\ ...

  7. 在Django中进行注册用户的邮件确认

    之前利用Flask写博客时(http://hbnnlove.sinaapp.com),我对注册模块的逻辑设计很简单,就是用户填写注册表单,然后提交,数据库会更新User表中的数据,字段主要有用户名,哈 ...

  8. django中tinymce添加图片上传功能

    主要参考以下: https://pixabay.com/en/blog/posts/direct-image-uploads-in-tinymce-4-42/ http://blog.csdn.net ...

  9. django中migration文件是干啥的

    昨天很蠢的问leader git push的时候会不会把本地的数据库文件上传上去,意思是django中那些migration文件修改之后会不会上传. 然后得知不会,因为所有的数据库都存在本机的mysq ...

随机推荐

  1. jar命令:打包、查看、更新等

    如何把写好的Java程序打包为jar文件呢?下面说的就是java使用命令行打包JAR的方法 1.命令行的方式:打包jar cf JAR文件名称 程序文件名称或者程序所在的文件夹举例:jar cf My ...

  2. C++学习007-使用exit退出进程

    使用exit可以实现退出当前进程. 如下 在程序接收到一个字符后,就退出进程 编写环境 vs2015 int main() { int a = 10, b = 20; std::cout <&l ...

  3. CSS3 : transition 属性

    CSS3的 transition 属性用于状态过度效果! 1.语法: transition: property duration timing-function delay; -moz-transit ...

  4. 【转】Linux学习(1)-常用快捷键、文件管理和查询

    原文链接:http://www.cnblogs.com/zhaopei/p/7397402.html 有话要说 为什么要用Linux?要用Linux的原因太多,想说说不完啊. 如果你说用Linux只是 ...

  5. Visual Studio Code 配置Go 开发环境最简单的方法!!!

    由于大家都知道的原因,在国内如果想访问go等各种资源,都会遇到某种不可预知的神奇问题.导致在VS Code中安装 go 各种插件都会失败. 于是乎,网上就出现了各种各样的解决方案:什么手动git cl ...

  6. 剑指offer-树的子结构17

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) class Solution: def issubTree(self,pRoot1,pRoot2) ...

  7. Ubuntu18.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook深度学习环境配置

    目录 一.Ubuntu18.04 LTS系统的安装 1. 安装文件下载 2. 制作U盘安装镜像文件 3. 开始安装 二.设置软件源的国内镜像 1. 设置方法 2.关于ubuntu镜像的小知识 三.Nv ...

  8. day-10 sklearn库实现SVM支持向量算法

    学习了SVM分类器的简单原理,并调用sklearn库,对40个线性可分点进行训练,并绘制出图形画界面. 一.问题引入 如下图所示,在x,y坐标轴上,我们绘制3个点A(1,1),B(2,0),C(2,3 ...

  9. struts2中的action为什么要继承ActionSupport类,不继承也可以,有什么好处?

    简单来说,有很多相关的方法都加载进来,你直接调用就行了,而且在安全上和稳定性上做了很好的处理 实际上继承ActionSupport之后 就等同于实现了很多接口 Action,Validateable, ...

  10. django之上传文件和图片

    文件上传:文件上传功能是网站开发中必定会使用到的技术,在django项目中也是如此,下面会详细讲述django中上传文件的前端和后端的具体处理步骤: 前端HTML代码实现: 1.在前端中,我们需要填入 ...