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 ...
随机推荐
- 原生js实现五子棋
为什突然做这个,因为这是个笔试题,拖了一个月才写(最近终于闲了O(∩_∩)O),废话不多说,说说这个题吧 题目要求 编写一个单机[五子棋]游戏,要求如下: 1.使用原生技术实现,兼容 Chrome 浏 ...
- 在Android上,怎样与Kotlin一起使用Retrofit(KAD21)
作者:Antonio Leiva 时间:Apr 18, 2017 原文链接:https://antonioleiva.com/retrofit-android-kotlin/ 这是又一个例子,关于怎样 ...
- LINQ学习笔记——(3)基本查询操作符
Select() 作用于uIEnumerable<TSource>类型 public static void Test() { List<string> persons = n ...
- EasyUI 布局 - 动态添加标签页(Tabs)
首先导入js <link rel="stylesheet" href="../js/easyui/themes/default/easyui.css"&g ...
- webstorm-前端javascript开发神器中文教程和技巧分享(转)
webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载: 百度网盘下载:http://pan.baidu ...
- 教你一步一步在linux中正确的安装Xcache加速php
#第一步,下载Xcache wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz #第一步非常简单,如果你下载不 ...
- 【Python】Python—判断变量的基本类型
type() >>> type(123)==type(456) True >>> type(123)==int True >>> type('ab ...
- 配置apache反向代理进行跨域
配置apache反向代理 打开配置文件httpd.conf 开启 proxy_http_module 和 proxy_module 模块,将#号删除 #LoadModule proxy_module ...
- MongoDB 存储日志数据
MongoDB 存储日志数据 https://www.cnblogs.com/nongchaoer/archive/2017/01/11/6274242.html 线上运行的服务会产生大量的运行及访问 ...
- javascript中window.location.search方法简介
window.location.search方法是截取当前url中"?"后面的字符串,示例如下: 例如:http://www.firefoxchina.cn/?ntab截取后的字符 ...