Gunicorn在被主管调用时抛出打开文件的错误

2024-10-03 23:23:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序,它呈现一个表单,并生成一个带有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'))

完整的回溯:

^{pr2}$

这是不寻常的,因为当我使用Django dev服务器测试应用程序时,或者使用Gunicorn从命令行调用Gunicorn时wsgi:app--文件返回成功。服务器总是在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

Tags: apps文件pathprojecthomepdfosubuntu