我如何通过 subprocess.Popen 和 mod_wsgi 使用命令行工具(即 sox)?
我有一个自定义的 django 文件字段,它使用 sox(一个命令行音频工具)。只要我使用 django 开发服务器,这就可以很好地工作。但是一旦我切换到生产服务器,使用 apache2 和 mod_wsgi,mod_wsgi 就会捕获到标准输出的每个输出。这使得无法使用命令行工具来评估文件,例如使用它来检查上传的文件是否确实是音频文件,如下所示:
filetype=subprocess.Popen([sox,'--i','-t','%s'%self.path], shell=False,\
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(filetype,error)=filetype.communicate()
if error:
raise EnvironmentError((1,'AudioFile error while determining audioformat: %s'%error))
是否有办法解决此问题?
编辑
我得到的错误是“缺少文件名”。我使用的是 mod_wsgi 2.5,标准的 ubuntu 8.04。
编辑2
当我在 mod_wsgi 的 django 中调用 subprocess.Popen 时,到底发生了什么?子进程 stdin/stdout 不应该独立于 django stdin/stdout 吗?在这种情况下,mod_wsgi 不应影响通过子进程调用的程序。是否可以使用 mod_wsgi 中的命令行工具?
I have a custom django filefield that makes use of sox, a commandline audiotool. This works pretty well as long as i use the django development server. But as soon as i switch to the production server, using apache2 and mod_wsgi, mod_wsgi catches every output to stdout. This makes it impossible to use the commandline tool to evaluate the file, for example use it to check if the uploaded file really is an audio file like this:
filetype=subprocess.Popen([sox,'--i','-t','%s'%self.path], shell=False,\
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(filetype,error)=filetype.communicate()
if error:
raise EnvironmentError((1,'AudioFile error while determining audioformat: %s'%error))
Is there a way to workaround for this?
edit
the error i get is "missing filename". I am using mod_wsgi 2.5, standard with ubuntu 8.04.
edit2
What exactly happens, when i call subprocess.Popen from within django in mod_wsgi? Shouldn't subprocess stdin/stdout be independent from django stdin/stdout? In that case mod_wsgi should not affect programms called via subprocess. Is it possible to use a commandlinetool like that from mod_wsgi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将调试添加到程序中,以将“self.path”的值记录到 stderr,以确保它实际上已设置为某些内容。消息“缺少文件名”表明它可能为空。另请注意,在 Apache/mod_wsgi 上运行时,必须使用文件的绝对路径名,因为当前工作目录不会像 Django 开发服务器那样是项目目录。最后,Apache 作为特殊用户运行,因此它需要对您需要访问/写入的目录具有适当的读取和/或写入访问权限。路径和访问问题记录在:
http://code.google.com/p/ modwsgi/wiki/ApplicationIssues
顺便说一句,对于标准输出问题,您确实应该升级到 mod_wsgi 3.3。阅读:
http://blog.dscpl .com.au/2009/04/wsgi-and-printing-to-standard-output.html
Add debug to your program to log to stderr the value of 'self.path' to ensure it is actually set to something. The message 'missing filename' suggest it may be empty. Also be aware that when running on Apache/mod_wsgi you must use absolute path names to files because the current working directory will not be the project directory like with the Django development server. Finally, Apache runs as a special user, so it needs to have appropriate read and/or write access to the directories you need it to access/write to. The path and access issues are documented in:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues
BTW, for stdout issues, you really should upgrade to mod_wsgi 3.3. Read:
http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html