首页 > 解决方案 > 无法从 php mac 执行长时间运行的 python 脚本

问题描述

我正在使用 php exec 调用 python 脚本来执行。python脚本有一个ncbiwww在线爆破方法,执行需要30多秒。我将maximum_execute_timephp.ini 中的内容编辑为 720 秒。短时间运行的 python 脚本给了我输出(我正在将输出写入一个 json 文件),长时间运行的脚本没有给我输出(没有写入文件)。我的代码如下:

爆炸.php:

if (!empty($_POST['inputFromHtml'])) {
        $python = (exec("path-to-python-script/pythonScript.py &")); //**this is not executing**
        $array = array('inputFromHtml' => $variable);
        $fp = fopen('path-to-json-file/inputWrittenToJson.json', 'w'); //**this part works**
        fwrite($fp, json_encode($array, JSON_PRETTY_PRINT));
        fclose($fp);


        if ($python) {
            echo "Successful! <br />"; //this not executing
            // echo "Accession number: " . $variable . "<br>"; //**not echoing**
            echo $python; // **not outputting**

            $URL = "http://localhost/folder/blast.php"; //**page refreshing but appears to be waiting for output forever**

            echo "<script type='text/javascript'>window.location.href = '{$URL}';</script>";
            echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">'; 
        }
);
    } elseif (alternative code) {
        other methods
    }

我已经搜索了几个论坛,但还没有解决方案。如果我使用终端(mac 用户),我的 python 脚本将完全执行。

我的python脚本:

import json, xmltodict, pprint, datetime, os, sys,
from flask import Flask,
from flask import request,
from Bio.Blast import NCBIWWW

with open('path-json-file-with-my-variable/inputWrittenToJson.json') as json_file:
    acc_dict = json.load(json_file)
    acc = acc_dict['accessionNumber']
    print(acc)

result_handle = NCBIWWW.qblast("blastn", "nr", acc, format_type="XML")

data = xmltodict.parse(result_handle.read(), process_namespaces=True)

pp = pprint.PrettyPrinter(indent=4)

with open('path-to-save-blast-output/blastOutput.json', 'w') as fp:
    pp.pprint(json.dump(genomeData, fp))

标签: phppython

解决方案


推荐阅读