【问题标题】:Having trouble using subprocess使用子进程时遇到问题
【发布时间】:2014-01-06 06:54:18
【问题描述】:

好的,我正在用 Django 构建一个网站,我需要使用一个用 Ruby 编写的 API。我已经下载了 API 并编写了一个 ruby​​ 脚本。现在我正在使用子进程 POpen 方法从 Django 调用脚本。这是我的 Django 视图中的内容:

def call(request):
   context = RequestContext(request)
   if request.method == 'POST':
        form = sendCall(request.POST)
        if form.is_valid():
          form.save(commit=True)
          p=subprocess.Popen("cd verboice-api-ruby/lib;ruby \"verbyTest.rb\"",shell=False, stdout=subprocess.PIPE)                              
          output, errors = p.communicate()                        
          return mainIndex(request)
    else:
        print form.errors
   else:
     form = sendCall()
   return render_to_response('main/call.html', {'form': form}, context)

我运行服务器并激活呼叫视图。 ruby 脚本将发送一个调用,然后返回主页。但是在调用实际的 ruby​​ 脚本时它给了我一个错误。它给了我这个错误:

OSError at /main/call/
[Errno 2] No such file or directory
Request Method: POST
Request URL:    http://127.0.0.1:8000/main/call/
Django Version: 1.5.4
Exception Type: OSError
Exception Value:    
[Errno 2] No such file or directory
Exception Location: /anaconda/lib/python2.7/subprocess.py in _execute_child, line 1308
Python Executable:  /anaconda/bin/python
Python Version: 2.7.5
Python Path:    
['/Users/manavdutta/Downloads/savinglives',
 '/anaconda/lib/python27.zip',
 '/anaconda/lib/python2.7',
 '/anaconda/lib/python2.7/plat-darwin',
 '/anaconda/lib/python2.7/plat-mac',
 '/anaconda/lib/python2.7/plat-mac/lib-scriptpackages',
 '/anaconda/lib/python2.7/lib-tk',
 '/anaconda/lib/python2.7/lib-old',
 '/anaconda/lib/python2.7/lib-dynload',
 '/anaconda/lib/python2.7/site-packages',
 '/anaconda/lib/python2.7/site-packages/PIL',
 '/anaconda/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time:    Sun, 5 Jan 2014 16:46:38 -0600

我应该怎么做才能阻止这个错误?

【问题讨论】:

    标签: python ruby django


    【解决方案1】:

    在我看来你使用 Popen 不正确:

    p = subprocess.Popen(['ruby', 'verbyTest.rb'], cwd='verboice-api-ruby/lib', shell=False, stdout=subprocess.PIPE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多