Subprocess.Popen从Django内部调用时挂起交互式程序 [英] Subprocess.Popen hangs with interactive programs when called from inside Django

查看:391
本文介绍了Subprocess.Popen从Django内部调用时挂起交互式程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小型的Django App,它基于用户输入执行交互式程序,并返回输出结果。但是由于某些原因,子进程挂起。在验证日志的时候,我发现一个地方必须给出一个\来作为对挑战的回应,这个回应似乎从来没有被做过。有趣的是,如果我从Django外部运行相同的代码,即从python模块或者从交互式shell运行相同的代码,子进程可以无阻碍地运行。我假设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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆