在运行我的Django项目时,我遇到了一个关于subprocess.CalledProcessError
的奇怪问题,这是在一个运行Apache的生产服务器上:
我的代码(更新:添加了catch-all-exception处理 - 行为没有改变)如下:
try:
command_string = 'gcc -O0 -g3 -Wall -c -fmessage-length=0 ' + cfile + ' -o ' + ofile
compile_result = subprocess.check_output(command_string,stderr=subprocess.STDOUT,shell=True)
#logger.warning(compile_result)
if compile_result != "": #Dann gab es einen Fehler bzw. ein Compiler-Warning --> Abbruch!
self.ausgabe = u"Compile:\n"
self.ausgabe += unicode(compile_result, "utf-8")
return
except subprocess.CalledProcessError as e:
self.ausgabe = u"Compilierfehler (Returncode {0}):\n".format(e.returncode)
self.ausgabe += unicode(e.output, "utf-8")
logger.error("CPE" + unicode(e.returncode, "utf-8") + unicode(e.output, "utf-8"))
return #die weiteren Schritte müssen gar nicht erst ausgeführt werden...
except:
logger.error(str(sys.exc_info()))
self.ausgabe = u"Compilieren nicht erfolgreich. Fehler:\n" + unicode(sys.exc_info(), "utf-8")
return
当我在我的Windows开发机和djange测试服务器上运行它时,所有的工作都符合预期。当命令执行失败时,异常被捕获,错误处理也按预期工作。
将代码移动到我的生产服务器(ubuntu,apache)时,当命令执行失败时,会出现“内部服务器错误500”,这不是期望的行为。Apache错误日志并不是很有用,因为它没有显示任何错误。
我的配置是: Apache/2.2.22(Ubuntu)PHP/5.4.9-4ubuntu2.3 mod_wsgi/3.4 Python/2.7.4
(是的,我重新启动了Apache,并确信我运行的是相同的代码。)
对此有什么想法吗?
try
块之外。500甚至可能是由于完全不属于Python的错误引起的。 - Martijn Pieters