ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 当supervisord调用时,Gunicorn会抛出OSError打开文件

2019-06-25 08:43:29  阅读:248  来源: 互联网

标签:python django gunicorn supervisord pdflatex


我有一个应用程序,它提供一个表单并使用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 dev服务器测试应用程序,或者使用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 debug的本地变量:

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的绝对路径进行调用才能找到它.

标签:python,django,gunicorn,supervisord,pdflatex
来源: https://codeday.me/bug/20190625/1284440.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有