【问题标题】:Why does Python 2.7 os.system(command) sometimes recurse in Windows 8 while os.startfile(command) does not recurse?为什么 Python 2.7 os.system(command) 有时会在 Windows 8 中递归,而 os.startfile(command) 不递归?
【发布时间】:2017-10-12 22:00:54
【问题描述】:

为什么 Python 2.7 os.system(command) 有时会在 Windows 8 中递归,而 os.startfile(command) 不递归?

我的命令是backup.bat,这是一个 Windows 批处理文件。

backup.bat的内容是:

"C:\Users\Frank Chang\Anaconda2\python.exe" -m animation_mini

@echo off
echo %time%
timeout 10 > NUL
echo %time%

我发现animation_mini.py 中的Python 2.7 animate 函数在os.system 时被多次调用的方式 使用的是在animate函数入口点的开头放置一个打印语句,并在控制台中统计打印语句。

今天有人告诉我 Python 2.7 os.system(command) 是 C 函数 execve 的包装器。但是这个事实并不能解释我在os.system('backup.bat') 中看到的递归。

os.system 是从 adder.cgi 调用的,这是一个 Python 2.7 CGI 脚本,其代码行是:

#!C:\Users\Frank Chang\Anaconda2\python.exe
import cgitb
import cgi 
import os    
import signal
import threading , time
import sys
sys.path.insert(0,"C:\Users\Frank Chang\Documents\Arduino\mary\data\usr\lib\python2.7\dist-packages\HTMLgen")

import HTMLgen
import subprocess
import win32api
import pandas as pd

def main(): 


    form = cgi.FieldStorage()

    numStr1 = form.getfirst("input1", "0") 
    numStr2 = form.getfirst("input2", "0") 
    numStr3 = form.getfirst("input3", "0") 
    numStr4 = form.getfirst("input4", "0") 
    numStr5 = form.getfirst("input5", "0") 
    numStr6 = form.getfirst("input6", "0") 
    numStr7 = form.getfirst("input7", "0") 

    numStr8 = form.getfirst("input8", "0") 

    numStr9 = form.getfirst("input9", "0") 

    numStr10 = form.getfirst("input10", "0") 

    numStr11 = form.getfirst("input11", "0") 

    numStr12 = form.getfirst("input12", "0") 

    numStr13 = form.getfirst("input13", "0") 

    numStr14 = form.getfirst("input14", "0") 

    from pandas import ExcelWriter

    writer = ExcelWriter('PythonExport.xlsx')

    from pandas import DataFrame

    yourdf = DataFrame({'DC Start': numStr1, 'DC Duration': numStr2, 'Plant Start': numStr3, 'Plant Duration': numStr4,
'Supplier Start': numStr5, 'Supplier Duration': numStr6}, index=[0])
    yourdf.to_excel(writer,'Disruptions')

    yourdf = DataFrame({'FGI': numStr10, 'WIP': numStr11, 'DC': numStr12, 'Plant': numStr13,
'Supplier' : numStr14}, index=[0])
    yourdf.to_excel(writer,'Policy')

    writer.save()

    os.system('backup.bat')

def processInput(numStr1, numStr2):  
    '''Process input parameters and return the final page as a string.'''
    num1 = int(numStr1) # transform input to output data
    num2 = int(numStr2)
    total = num1+num2
    return str(total)

def fileToStr(fileName): 
    """Return a string containing the contents of the named file."""
    fin = open(fileName); 
    contents = fin.read();  
    fin.close() 
    return contents

main()

我的 CGI 脚本可能是 os.system('backup.bat') 递归的原因吗?

【问题讨论】:

  • 你能澄清一下这里的“递归”是什么意思吗?当它没有按您预期的方式工作时,实际发生了什么?你怎么知道它在做什么?
  • 您从哪里运行os.system 命令?是在animation_mini.py 文件中,还是在其他脚本中?能否展示相关的 Python 代码(而不是不太相关的批处理脚本代码)?
  • os.system 不是 execve 的包装。它调用system,在Windows上通过cmd.exe /c command执行命令。
  • @Blckknght,我可能在这里松散地使用“recurse”关键字。我的意思是,当我查看 CMD 窗口中的打印流活动查看 Python 2.7 animation_mini.py 代码时,python 2.7 animate() 函数在 4 分钟的时间内至少被调用了 4 次。我希望它应该只被调用一次,就像 os.startfile('backup.bat') 一样。
  • @FrancisTuan, os.startfile 将在新控制台中运行批处理文件,当附加的 cmd 和 Python 实例退出时,该批处理文件将被销毁。您是否有某种 cmd pause 或 Python input 语句来保持控制台打开以实际查看它是否通过 os.startfile 多次运行?如果没有,请在批处理文件末尾添加pause 语句。

标签: python windows python-2.7 recursion


【解决方案1】:

eryksun 和 Blckkngt 昨天都给出了完全正确的答案,即将多个版本混合在一起导致递归噩梦。

INTERNAL ERROR: cannot create temporary directory 的解决方法在于 pyinstaller 可以通过使用 win32api.SetDllDirectory(..) 并复制父进程的环境变量并添加键值对 "TEMP", "C :/TMP" 到字典中

CGI 脚本与我们的动画函数是否连续运行多次无关。

【讨论】:

  • @eryksun,我可以请你批评这个答案吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-04-25
  • 2022-09-28
  • 2013-11-25
  • 2022-11-10
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2016-07-11
相关资源
最近更新 更多