无法在php中多次运行python脚本

时间:2018-07-13 01:48:51

标签: php python ajax codeigniter

我有一个像这样的ajax代码:

function getbound(pitvalue){
    $.ajax({
        url : "Begesitbor/WriteSite",
        type : "POST",
        dataType : "json",
        data : {"site" : String(pitvalue)},
        success : function(response) {
        responsestr = String(response);
        result = responsestr.split(",");
        alert(responsestr);
        },
        error: function(errorThrown) {
            console.log(errorThrown);
        }
    });
}

这是其控制器中的php代码:

public function WriteSite(){
    $site = $this->input->post('site');
    $output=explode(" ",shell_exec(escapeshellcmd("python E:\boundogrsitebegesit.py data_log_bor $site")));
    echo json_encode($output);
}

这是python编码:

    import ogr
    import sys

databaseServer = "localhost"
databaseName = "postgis"
databaseUser = "postgres"
databasePW = "lontong"
connString = "PG: host=%s dbname=%s user=%s password=%s"%(databaseServer,databaseName,databaseUser,databasePW)

def GetPGBound( lyr_name, pit_name ):
    try:
        conn = ogr.Open(connString)
        lyr = conn.GetLayer( lyr_name )
        if lyr is None:
            print >> sys.stderr, '[ ERROR ]: layer name = "%s" could not be found in database "%s"' % ( lyr_name, databaseName )
            sys.exit( 1 )
        lyr.SetAttributeFilter("site = \'"+pit_name+"\'")
        Xarr=[]
        Yarr=[]
        for feature in lyr: #inLayer is always of size one because polygon is a unique value
            if (feature.GetField("e_plan")!=None)and(feature.GetField("n_plan")!=None):
                Xarr.append(feature.GetField("e_plan"))
                Yarr.append(feature.GetField("n_plan"))
        featureCount = lyr.GetFeatureCount()
        conn = None
        return min(Xarr), min(Yarr), max(Xarr), max(Yarr)
    except: 
        text_file = open("E:\\boundogrbegesit_log.txt", "w")
        text_file.write("Unexpected error : "+ str(sys.exc_info()[0]))
        text_file.close()
        raise
if __name__ == '__main__':  
    try:
        if len( sys.argv ) < 3:
            print >> sys.stderr, '[ ERROR ]: you must pass at least two argument -- the python script file and sector name'
            text_file = open("E:\\boundogr_log.txt", "w")
            text_file.write("Unexpected error : "+ str(sys.exc_info()[0]))
            text_file.close()
            sys.exit( 1 )
        lyr_name = sys.argv[1]
        pit_name = sys.argv[2]
        (minX, maxX, minY, maxY) = GetPGBound( lyr_name, pit_name )
        print minX, maxX, minY, maxY
    except:
        text_file = open("E:\\boundogr_log.txt", "w")
        text_file.write("Unexpected error : "+ str(sys.exc_info()[0]))
        text_file.close()
        raise
    exit()

我们可以看到$ output是由php控制器运行的python脚本产生的。如果更改了选择(下拉)对象或加载了网页,则将运行getbound(pitvalue)。如果更改了选择(下拉)对象,则警报某种程度上为空,这意味着响应值也为空。如果我将echo json_encode($ output)更改为echo json_encode($ site),则响应不为空,并且如果加载了网页或更改了select(下拉)对象,则会显示响应。因此,我认为python脚本不能不能运行两次,而只能在开始(网页加载)中运行一次。如何使python脚本不仅在网页加载时运行多次,而且还运行多次?

为您提供信息,我从python日志文本文件中得到了这样的错误:

Unexpected error : <type 'exceptions.SystemExit'>

0 个答案:

没有答案