我有一个内置的cherrypy服务器上运行的web.py应用程序。
我想在用户发布到url时执行一个外部脚本,该脚本将在python子进程的后台运行.Popen调用和web.py将重定向到另一个页面,在该页面中使用jquery监视脚本的进度ajax请求。
但是我无法在这里正确实现。
代码段如下,如果需要,我可以提供更多信息。
import web
from multiprocessing import Process
import subprocess, shlex
import time, json
from login import authorize, sessidGen
def __callProcess(processString,mod='w',shell=False):
if not shell: args = shlex.split(processString)
else: args = processString
out = open('./bteq/logs/output.log',mod)
err = open('./bteq/logs/error.log',mod)
p = subprocess.Popen(args,stdout=out,stderr=err,shell=shell)
return p
def setExec():
__callProcess("chmod +x ./bteq/*.sh",shell=True)
def bteqExec(filename,system):
if system not in ['prod','da','cdw','cdw2','cert','']: return False
processString = " ".join([filename,system])
p = __callProcess(processString)
return p.pid
render = web.template.render('templates/',base='layout')
render_plain = web.template.render('templates/')
class Executor:
def GET(self):
authorize()
session = web.ctx.session
inputs = web.input(sessid={},type={})
if not inputs.sessid or session.id != inputs.sessid: web.seeother('/')
if inputs.sessid and inputs.type:
return render.executor('BTEQ Executor',inputs.type,inputs.sessid)
else: raise web.seeother('/')
def POST(self):
authorize()
session = web.ctx.session
inputs = web.input(sessid={},type={},act={})
if not inputs.sessid or session.id != inputs.sessid: web.seeother('/')
if inputs and inputs.act == 'start':
pid = bteqExec('python ./bteq/timer.py','')
session.id = sessidGen()
session.exctrpid = pid
return web.seeother('/progress.htm')
else: raise web.seeother('/')
class progress:
def GET(self):
authorize()
session = web.ctx.session
inputs = web.input(ajax={})
if inputs.ajax == 'true':
web.header('Content-Type', 'application/json')
if session.count >= 100: session.count = 0
session.count += 10
pid = session.exctrpid
out = open('./bteq/logs/output.log','r')
err = open('./bteq/logs/error.log','r')
output = ('<strong>OUTPUT:</strong><br>'+out.read()).replace('\n','<br>')
err = err.read()
if err:error = ('<strong>ERRORS:</strong><br>'+err.read()).replace('\n','<br>')
else: error = None
d = {'count':session.count,'msg':output,'err':error,'rc':pid,'session_id':session.session_id}
return json.dumps(d)
r = web.template.Template('$def with (title)\n$var title: $title\n')
return render_plain.layout_pgbar(r('progress test'))
由于subprocess.Popen对象不可腌制,因此无法将其作为会话变量放入,并且我想从进度类中获取p.poll()和p.stdout.read()。
我也想让代码在Linux和Windows中都可运行,我在Windows中安装了我的开发程序,并将其部署在Linux服务器上。
有人可以帮我吗...
谢谢。
参考方案
我将其放入一个多处理流程中,并从生成的流程中为子流程执行p.wait(),生成的流程会处理其余步骤并将结果更新到数据库中。
web.py进度页面将在数据库中签入执行进度,因此已解决。
码:
from multiprocessing import Process
class ProcHandler(Process):
def __init__(self, *args, **kwargs):
#Initialize
Process.__init__(self, *args, **kwargs)
def run(self):
p = bteqExec('python ./bteq/timer.py','')
rc = p.wait()
# do update the resulting to database. and make the web.py
# Progress class read from database entry made by this Process.
return
使用python请求进行网页爬取 - python我想抓取https://sparrow.eoffice.gov.in/IPRSTATUS/IPRFiledSearch并下载截至日期显示在搜索结果中的整套PDF文件(例如2016年1月1日)。员工字段是可选的。单击搜索后,该网站将列出所有员工的列表。我无法使用python请求获取post方法。持续收到405错误。我的代码如下from bs4 import B…
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python ThreadPoolExecutor抑制异常 - pythonfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…