升级版

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. Scala学习笔记(一):环境搭建

    计算机领域的编程语言种类繁多,如C.C++.Java.C#等,我们知道的一般都是较为流行的编程语言,然有更多的是没听说过的,于是也就说不上关注或者使用了 一次在网上查资料时,无意间看到了“函数式编程” ...

  2. 数据库sql命令

    本文为转载,原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4530333.html 1.说明:创建数据库CREATE DATABASE databas ...

  3. 1072 Gas Station (30 分)(最短路径)

    #include<bits/stdc++.h> using namespace std; ; int n,m,k,Ds; int mp[N][N]; int dis[N]; int vis ...

  4. python基础训练营03——字典、集合、判断、循环

    一.字典dict: 相比列表list而言,列表list像一本书,如果要查书中的某一个内容,需要把书从前往后翻一遍,直到找到想要获取的东西:而字典dict,就像现实中的字典一样,通过查找特定的字或者词( ...

  5. node + npm 命令

    npm install npm@latest -g  //更新npm npm -v  //运行查看版本号 地址:https://docs.npmjs.com/getting-started/insta ...

  6. [转]Hexo博客添加访问统计 - 记录

    引入不蒜子 <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"> ...

  7. Spring框架(依赖注入)

    特点 1轻量级和侵入性低 2依赖注入和面向接口实现松耦合 3面向切面编程 减少样式代码 专有名词: 1依赖注入:对象无需自行管理依赖关系.通过系统负责协调在创建对象的第三方组件的设定,实现依赖关系自动 ...

  8. sc"

    2.11 题目:二叉搜索树中的最近公共祖先 2.12 设计思路 if 树中不存在 u 或 v 错误 结束程序 定义 p 指向根节点 while true do: if p->key大于 u 和 ...

  9. SpringBoot 入门学习(HelloWord)

    前置知识 1.会利用 maven 构建项目 2.了解 Spring 注解 3.了解 RESTful API 的基本理论 4.SpringBoot 是 SpringMVC 的升级版,但两者没有必然的联系 ...

  10. hibernate笔记(三)

    目标: 第1部分: 对象的状态: 第2部分:缓存 1) 一级缓存 2) 相关知识 ----懒加载--- 第3部分:映射 一对一映射 组件映射 继承映射 一.对象的状态 举例: User   user  ...