Python web.py 异步执行外部程序

0 投票
1 回答
1126 浏览
提问于 2025-04-17 13:51

我有一个用web.py写的应用程序,它运行在内置的cherrypy服务器上。
我想在用户向一个网址发送请求时执行一个外部脚本,这个脚本会在后台运行,使用python的subprocess.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服务器上部署。

有没有人能帮我一下……

谢谢。

1 个回答

0

我把它做成了一个多进程进程,然后在这个新进程里调用p.wait()来等待子进程的完成,接着这个子进程会处理后面的步骤,并把结果更新到数据库里。

网页的进度页面会在数据库里查看执行的进度,这样问题就解决了。

代码:

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

撰写回答