python - Gunicorn 在被 supervisord 调用时抛出 OSError 打开文件

标签 python django gunicorn pdflatex supervisord

我有一个应用程序,它呈现一个表单并生成一个带有 pdflatex 的 PDF 文件,该文件作为文件附件返回给浏览器。当我手动调用应用程序服务器时它可以工作,但是当服务器进程由 Supervisord 启动时,它会中断......

Django 抛出一个 OSError:

[Errno 2] No such file or directory
Request Method: POST
Request URL:    http://apps.xxxxxxxx.com/pdf/view/1/85/
Django Version: 1.4.5
Exception Type: OSError
Exception Value:    
[Errno 2] No such file or directory
Exception Location: /usr/lib/python2.7/subprocess.py in _execute_child, line 1249
Python Executable:  /home/ubuntu/Envs/venv/bin/python
Python Version: 2.7.3

此行引发错误:subprocess.call(shlex.split(proc_string), stdout=open(os.devnull, 'wb'))

完整的追溯:

Traceback:
File "/home/ubuntu/Envs/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/CURRENT/project/appname/apps/catpdf/views.py" in render_preview
  114.             subprocess.call(shlex.split(proc_string), stdout=open(os.devnull, 'wb'))
File "/usr/lib/python2.7/subprocess.py" in call
  493.     return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py" in __init__
  679.                             errread, errwrite)
File "/usr/lib/python2.7/subprocess.py" in _execute_child
  1249.                 raise child_exception

Exception Type: OSError at /pdf/view/1/85/
Exception Value: [Errno 2] No such file or directory

这是不寻常的,因为当我使用 Django 开发服务器测试应用程序时,或使用 gunicorn wsgi:app 从命令行调用 Gunicorn 时——文件已成功返回。服务器总是在 nginx 后面。

这是抛出错误的代码部分(subprocess.call 行):

        output = t.render(cont)
        oname = ''.join([slugify(context['qb_full_name']), datetime.datetime.now().strftime("%Y%m%d_%H%M")])
        out_f = open(''.join([os.path.join(rnddir, oname), '.tex']), "w")
        out_f.write(output.encode('utf-8'))
        out_f.close()
        #jname = ''.join(['-jobname=', oname])
        proc_string = ' '.join(['pdflatex', '-output-directory', rnddir, os.path.abspath(out_f.name)])
        subprocess.call(shlex.split(proc_string), stdout=open(os.devnull, 'wb'))
        fname = os.path.join(rnddir, ''.join([oname, '.pdf']))
        pdf = open(fname, 'r')
        for s in signatures:
            os.unlink(s)
        response = http.HttpResponse(FixedFileWrapper(pdf), content_type=mimetypes.guess_type(fname)[0])
        response['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(fname)
        response['Content-Length'] = os.path.getsize(fname)
        return response

来自 Django 调试的本地变量:

out_f   
<closed file u'/home/ubuntu/CURRENT/project/appname/apps/catpdf/rendered/customer-name20130509_1541.tex', mode 'w' at 0x4211780>
signatures  
['/home/ubuntu/CURRENT/project/appname/apps/catpdf/tmp/tmpyJrUYO.pdf',
 '/home/ubuntu/CURRENT/project/appname/apps/catpdf/tmp/tmpLGMaRT.pdf']
rnddir
'/home/ubuntu/CURRENT/project/appname/apps/catpdf/rendered'
proc_string 
u'pdflatex -output-directory /home/ubuntu/CURRENT/project/appname/apps/catpdf/rendered /home/ubuntu/CURRENT/project/appname/apps/catpdf/rendered/customer-name20130509_1541.tex'

主管配置:

[program:gunicorn]
directory=%(ENV_HOME)s/CURRENT/project
user=ubuntu
command=gunicorn --preload wsgi:appname
environment=PATH="/home/ubuntu/Envs/venv/bin"
stdout_logfile = %(ENV_HOME)s/CURRENT/logs/guni-access.log
stderr_logfile = %(ENV_HOME)s/CURRENT/logs/guni-error.log
autostart=True
autorestart=True
priority=997

最佳答案

您正在覆盖 PATH 环境变量。您需要使用 pdflatex 的绝对路径以供 call 找到它。

关于python - Gunicorn 在被 supervisord 调用时抛出 OSError 打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16465934/

相关文章:

python - 如何创建配置文件并告诉 Gunicorn 使用它?

Python Uvicorn——获取SSL证书信息

python - OpenCV 2.4,Python-从SURF关键点检索laplacian

javascript - 相当于 Python 的 int ('hex-string' , 16) 在 Javascript 中?

python - 使用特定单词列表将单词更改为数字的最佳方法

某些文本中的日期的python正则表达式,由两个关键字括起来

python - Django不能上传文件和图片

python - Flask部署

django - 单元测试权限

python - 方法不允许(POST)Django 405 错误