Subprocess.Popen从Django内部调用时挂起交互式程序 [英] Subprocess.Popen hangs with interactive programs when called from inside Django
问题描述
def runtests(test_name,selective = False,tests_file ='') :
如果选择性:
run_cmd = ['runtest','--runfromfile',tests_file,test_name]
else:
run_cmd ='runtest%s'%(test_name)
print'执行命令'
print run_cmd
p = subprocess.Popen(run_cmd,shell = False,stdout = subprocess.PIPE,
stderr = subprocess.STDOUT)
return p.stdout.read()
def result(request):
test_name = request.GET ['test_name']
如果不是test_name:
return render_to_response('webrun / execute.html',{'error_flag':True})
in_file = os.path.abspath('webrun / log /%s_in.xml'% test_name)
suites = dict([(field [len('suite _'):],value)
for field,value in request.GET.items()
if fi eld.startswith('suite_')])
如果套件:
_from_dict_to_xml(套件,in_file,test_name)
output = runtests(test_name,bool(suites),in_file)
return render_to_response('webrun / result.html',{'output':output})
我已经尝试用旧的os.system方法替换子进程。但即使是在同一个地方。再次,如果我从Django执行相同的代码,这也运行。
我想你的问题是' runtest'程序位于。您的视图将在该视图所在的目录中获取该模块。如果模块在其他地方,您还可以在Popen参数列表中指定cwd参数。
我在使用Django开发服务器的视图中使用Popen命令,没有任何问题,所以开发服务器不是您的问题的原因。
I have written a small Django App, that executes an interactive program based on user input and returns the output as the result. But for some reason, the subprocess hangs. On verification of the logs I found that a place where a '\n' has to be given as response to a challenge, the response seems to have never been made. Interestingly, if I run the same code from outside of Django, i.e either from a python module or from the interactive shell, subprocess works without a hitch. I am assuming some settings within the environment used by Django are the culprit here. Here are snippets of the code that I've written:
def runtests(test_name, selective=False, tests_file=''):
if selective:
run_cmd = ['runtest', '--runfromfile', tests_file, test_name]
else:
run_cmd = 'runtest %s' % (test_name)
print 'Executing command .. '
print run_cmd
p = subprocess.Popen(run_cmd, shell=False, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return p.stdout.read()
def result(request):
test_name = request.GET['test_name']
if not test_name:
return render_to_response('webrun/execute.html', {'error_flag':True})
in_file = os.path.abspath('webrun/log/%s_in.xml' % test_name)
suites = dict([(field[len('suite_'):],value)
for field,value in request.GET.items()
if field.startswith('suite_')])
if suites:
_from_dict_to_xml(suites, in_file, test_name)
output = runtests(test_name, bool(suites), in_file)
return render_to_response('webrun/result.html', {'output':output})
I've tried replacing subprocess with the older os.system method. But even that hangs in the exact same place. Again, this runs too if I were execute same code out of Django.
I think your problem is the directory where the 'runtest' program is located. You view is going to fetch that module in the same directory where the view is. You can also specify 'cwd' argument in the Popen list of arguments if the module is anywhere else. I'm using Popen command in a view with Django dev server whithout any problems, so the dev server is not the cause of your problem.
这篇关于Subprocess.Popen从Django内部调用时挂起交互式程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!