出于某种原因,我需要从django视图签出一个源代码文件夹,并为此使用“Popen”。
一切都很好,在使用django runserver时工作得很好。
但是,在我将代码部署到apache2 + wsgi之后,Popen无法正常工作。它总是在命令实际完成之前返回。它也不会抛出一个错误,它只是抛出完整的输出,我检查了检出的文件夹,它们也是不完整的。
整个svn签出过程大约需要5-6秒,并且标准输出相当大(大约3000字符)。
我知道有一个pysvn库,但似乎很难在过时的ubuntu服务器上安装它。
基本上这是我现在唯一被困住的东西。
我用来调用签出的代码片段如下:
def run_cmd(argument_list, output_file = None):
print "arguments", argument_list
p = subprocess.Popen(argument_list, stdout=subprocess.PIPE)
content = ""
while True:
line = p.stdout.read(50)
if not line:
break
content += line
if output_file:
fout = file(output_file, "w")
fout.write(content)
fout.close()
return content
output = run_cmd(["/usr/bin/svn", "--ignore-externals", "co", svn_url, src_folder] )以下是一些可能有用的信息:
我已经被困在这几个小时了,任何提示都是非常感谢的!
谢谢。
发布于 2012-07-21 09:39:19
好吧,今天又挣扎了三个小时。我终于解决了这个问题。
下面是所发生的事情,wsgi & popen实际上很好,真正的问题是一些用于签出的源代码文件实际上有特殊的字符,并且打破了svn签出过程(有下面的错误)
svn:无法将字符串从'UTF-8‘转换为本机编码
wsgi和控制台对于LC_LANG具有不同的价值,这解释了runserver和wsgi之间的不同行为。
最后,我通过修改'/etc/apache2/envars‘文件解决了这个问题,并取消了以下行的注释:
。/etc/默认/地区
请注意,必须通过“apache2ctl stop”和“apache2ctl start”而不是“apache2ctl restart”重新启动服务器,才能使设置生效。
实际上,我使用帕普来找出问题,但由于明显的延迟问题,我后来又回到了Popen。
下面是我为run_cmd编写的最后代码,希望它能在未来帮助到其他人:
def run_cmd(argument_list, output_file = None):
print "arguments", argument_list
#command = " ".join(argument_list)
#content = pexpect.run(command)
#if output_file:
#fout = file(output_file, "w")
#fout.write(content)
#fout.close
#return content
p = subprocess.Popen(argument_list, bufsize=50, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) #showed error message as well
content = ""
while True:
line = p.stdout.read(50)
if not line:
break
content += line
#raise Exception(content) #for debug
if output_file:
fout = file(output_file, "w")
fout.write(content)
fout.close()
return contenthttps://stackoverflow.com/questions/11590409
复制相似问题