升级版

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. 【APUE】Chapter5 Standard I/O Library

    5.1 Introduction 这章介绍的standard I/O都是ISOC标准的.用这些standard I/O可以不用考虑一些buffer allocation.I/O optimal-siz ...

  2. 第二篇 Fiddler配置_浏览器&手机

    什么是Fiddler? 网络项目的开发和测试中,Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的 ,可以说是非常常用的手头工具了,本文就Fiddler使用和配置进行说明. ...

  3. SPRITEKIT游戏框架之关于PHYSICS物理引擎属性

    Spritekit提供了一个默认的物理模拟系统,用来模拟真实物理世界,可以使得编程者将注意力从力学碰撞和重力模拟的计算中解放出来,通过简单地代码来实现物理碰撞的模拟,而将注意力集中在更需要花费精力的地 ...

  4. LINUX系统下Java和Scala的环境配置

    最近,笔者在研究一个有关“自然语言处理”的项目,在这个项目中,需要我们用Spark进行编程.而Spark内核是由Scala语言开发的,所以在使用Spark之前,我们必须配置好Scala,而Scala又 ...

  5. mongodb数据库高级操作

    1.创建索引 2.索引名称 3.其他索引 4.explain 5.操作索引 6.高级特性 7.固定集合 8.导入导出 9.上锁 10.添加用户 11.主从复制

  6. HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  7. JSONP跨域jQuery处理整理(附天气数据实例)

    写在前面 跨域的解决方案有多种,其中最常见的是使用同一服务器下的代理来获取远端数据,再通过ajax进行读取,而在这期间经过了两次请求过程,使得获取数据的效率大大降低,这篇文章蓝飞就为大家介绍一下解决跨 ...

  8. Mininet简单性能测试

    建一个简单的模型,使用一个单交换机,然后链接n个主机形成拓扑,然后对每个链路设置带宽,延迟时间,和丢包率. 这里就选择建一个单交换机和六个主机的作为例子. 创建py脚本生成拓扑:写一个类生成一个单交换 ...

  9. 文件系统之 stat与access

    stat命令 stat既有命令也有同名函数,用来获取文件Inode里主要信息,所以stat命令的输出信息比ls命令的输出信息要更详细,stat 跟踪符号链接,lstat不跟踪符号链接,其中会输出对应文 ...

  10. 牛客网(string::find()函数回忆一下)

    链接:https://www.nowcoder.com/acm/contest/109/B来源:牛客网 给出两个串s和x 定义s中的某一位i为好的位置,当且仅当存在s的子序列 满足y=x且存在j使得i ...