从PHP执行Python脚本,计时问题?

时间:2015-12-13 01:15:07

标签: php python

我想我的脚本完成之前会有一些计时器到期。我找到了这个很好的教程。 How to execute python script from php and show output on browser。它开始工作,但在完成之前停止从脚本打印我的调试消息。

我的PHP代码:

<?php

$param1 = '"romAudit"';
$param2 = '"Nintendo Entertainment System"';
$param3 = "third";

$command = "python scripts/arcade_functions.py";
$command .= " $param1 $param2";

 header('Content-Type: text/html; charset=utf-8');
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
echo "<style type='text/css'>
 body{
 background:#000;
 color: #7FFF00;
 font-family:'Lucida Console',sans-serif !important;
 font-size: 12px;
 }
 </style>";



$pid = popen( $command,"r");

echo "<body><pre>";
while( !feof( $pid ) )
{
 echo fread($pid, 256);
 flush();
 ob_flush();
 echo "<script>window.scrollTo(0,99999);</script>";
 usleep(100000);
}
pclose($pid);

echo "</pre><script>window.scrollTo(0,99999);</script>";
echo "<br /><br />Script Complete<br /><br />";
?>

从Windows命令提示符输出:

C:\Arcade\www\scripts>python arcade_functions.py "romAudit" "Nintendo Entertainment System"
Arguments passed: romAudit Nintendo Entertainment System
Executing audit of Nintendo Entertainment System now

Extracting system XML database ...
Extraction Complete.

Creating a list of the files in the directory of ROM Path...
Directory Listing Complete

Determining which ROMs match the XML database ...
Found Directory: _Not In Database , Sub-Directories will not be audited
Matching Complete

Attempting to create "_Not In Database" directory
ERROR directory Y:\ROMS\Nintendo Entertainment System\_Not In Database, exists

Moving unmatched ROMs to "_Not In Database" directory ...
Moving complete, check Y:\ROMS\Nintendo Entertainment System\_Not In Database for un-matched ROMs

found 791 out of 791 ROMs

C:\Arcade\www\scripts>

我认为在完成之前有一些计时器或其他东西会杀死脚本。在Windows命令提示符下完成大约需要2.5秒。

我知道脚本没有在PHP之外完成,因为我创建了一个日志文件并在python函数的末尾写入了所有内容。

Python脚本(我还在学习,我删除了一些不适用于符合字符限制的其他功能):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Script Python Example

from xml.dom import minidom
import xml.etree.cElementTree as ET
import os, re, shutil, sys

'''
    Create the Log Files directories first by uncommenting the "createLogDirs()", only run this once
    Set all paths in the path sections below
'''

rlPath = 'C:\\Arcade\\RocketLauncher\\' #set your RocketLauncher Path here
hsPath = 'C:\\Arcade\\FrontEnds\\HyperSpin\\' #set your HyperSpin Path here
romPath = 'Y:\\ROMS\\'  #set your ROM Path here
downloadPath = "C:\\Users\\Ben\\Downloads\\EmuMovies\\" #EmuMovies Download Path
logFiles = 'C:\\Arcade\\Logs\\' #Where to create and put the log files

param1 = sys.argv[1]
param2 = sys.argv[2]

print('Arguments passed:', param1, param2)


def databaseGameExtraction(xml):
    print('Extracting system XML database ...')
    xmldoc = minidom.parse(xml)
    systemName = [xmldoc.getElementsByTagName('listname')[0].childNodes[0].data]
    games = xmldoc.getElementsByTagName('game')
    roms = []
    for game in games:
        romKey = game.attributes['name']
        roms.append(romKey.value)
    print('Extraction Complete.',"\n")
    return roms, systemName

def romsAvailable(path):
    print('Creating a list of the files in the directory of ROM Path...')
    romsList = os.listdir(path)
    print('Directory Listing Complete',"\n")
    return romsList

def romAudit(system):
    print('Executing ROM audit of', system, 'now', '\n')
    xmlToAudit = hsPath + 'Databases' + '\\' + system + '\\' + system + '.xml'
    if os.path.isfile(xmlToAudit):
        roms, systemName = databaseGameExtraction(xmlToAudit)
        romsList = romsAvailable(romPath + system)
        output = []
        print('Determining which ROMs match the XML database ...')
        for newList in romsList:
            if os.path.isdir(romPath + system + '\\' + newList):
                print('Found Directory:', newList, ', Sub-Directories will not be audited')
            else:
                newRoms = os.path.splitext(newList)
                output.append(newRoms)
        inDatabase = []
        notInDatabase = []
        for value in output:
            if value[0] in roms:
                inDatabase.append(value)
            else:
                notInDatabase.append(value)
        print('Matching Complete', "\n")
        notInDatabasePath = romPath + system + '\\' + '_Not In Database'
        print('Attempting to create "_Not In Database" directory')
        try:
            os.makedirs(notInDatabasePath)
        except:
            print('ERROR directory ' + notInDatabasePath + ', exists \n')
        print('Moving unmatched ROMs to "_Not In Database" directory ...')
        for extra in notInDatabase:  # Moves the ROMs that are not in the database
            dst = notInDatabasePath + '\\' + extra[0] + extra[1]
            src = romPath + system + '\\' + extra[0] + extra[1]
            try:
                # print(src)
                # print(dst)
                shutil.move(src, dst)
            except:
                errorMessage = 'error copying ' + '"' + extra[0] + extra[1] + '"' + '\n'
                file = open(logFiles + system + ' romAuditError.txt', 'a')
                errorlog = errorMessage
                file.write(errorlog)
                file.close()
        print('Moving complete, check', notInDatabasePath, 'for un-matched ROMs', "\n")
        print('found ' + str(len(inDatabase)) + ' out of ' + str(len(roms)) + ' ROMs')
    else:
        print('error' + system + 'not a valid XML File')

    if os.path.isfile(logFiles + 'Have\\' + system + ' ROM have.txt'):
        os.remove(logFiles + 'Have\\' + system + ' ROM have.txt')
    if os.path.isfile(logFiles + 'Missing\\' + system + ' ROM missing.txt'):
        os.remove(logFiles + 'Missing\\' + system + ' ROM missing.txt')
    have = []
    for game in inDatabase:
        rom = game[0] + '\n'
        have.append(game[0])
        file = open(logFiles + 'Have\\' + system + ' ROM have.txt', 'a')
        file.write(rom)
        file.close()
    missing = []
    for rom in roms:
        if rom not in have:
            missing.append(rom)
            print(rom, "NOT found")
    for miss in missing:
        output = miss + '\n'
        file = open(logFiles + 'Missing\\' + system + ' ROM missing.txt', 'a')
        file.write(output)
        file.close()



if param1 == 'romAudit':
    romAudit(param2)
else:
    print(param1, 'or', param2, 'is not valid')

PHP输出(从Chrome浏览器复制):

Arguments passed: romAudit Nintendo Entertainment System
Executing ROM audit of Nintendo Entertainment System now 

Extracting system XML database ...
Extraction Complete. 

Creating a list of the files in the directory of ROM Path...


Script Complete

0 个答案:

没有答案