努力将Python应用程序部署到heroku,以应对gunicorn配置

问题描述

我继承了一个python django应用程序,并试图为其带来新的生命。它当前正在使用python 2.7和许多最终需要升级的旧软件包。但是升级之前的第一件事是我希望它在heroku上成功运行。到目前为止,我在本地运行良好,但是遇到了Heroku和Gunicorn问题。

这是我遇到的错误:

2020-09-10T03:48:27.212247+00:00 heroku[web.1]: Starting process with command `gunicorn spotlit_due_diligence.spotlit_due_diligence.wsgi:application`
2020-09-10T03:48:29.477479+00:00 heroku[web.1]: State changed from starting to up
2020-09-10T03:48:29.206728+00:00 app[web.1]: [2020-09-10 03:48:29 +0000] [4] [INFO] Starting gunicorn 19.10.0
2020-09-10T03:48:29.207159+00:00 app[web.1]: [2020-09-10 03:48:29 +0000] [4] [INFO] Listening at: http://0.0.0.0:45741 (4)
2020-09-10T03:48:29.207253+00:00 app[web.1]: [2020-09-10 03:48:29 +0000] [4] [INFO] Using worker: sync
2020-09-10T03:48:29.211268+00:00 app[web.1]: [2020-09-10 03:48:29 +0000] [11] [INFO] Booting worker with pid: 11
2020-09-10T03:48:29.250434+00:00 app[web.1]: [2020-09-10 03:48:29 +0000] [13] [INFO] Booting worker with pid: 13
2020-09-10T03:48:29.000000+00:00 app[api]: Build succeeded
2020-09-10T03:48:30.075238+00:00 app[web.1]: [2020-09-10 03:48:30 +0000] [11] [ERROR] Exception in worker process
2020-09-10T03:48:30.075250+00:00 app[web.1]: Traceback (most recent call last):
2020-09-10T03:48:30.075251+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",line 586,in spawn_worker
2020-09-10T03:48:30.075251+00:00 app[web.1]: worker.init_process()
2020-09-10T03:48:30.075251+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py",line 135,in init_process
2020-09-10T03:48:30.075252+00:00 app[web.1]: self.load_wsgi()
2020-09-10T03:48:30.075252+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py",line 144,in load_wsgi
2020-09-10T03:48:30.075252+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-09-10T03:48:30.075253+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py",line 67,in wsgi
2020-09-10T03:48:30.075253+00:00 app[web.1]: self.callable = self.load()
2020-09-10T03:48:30.075254+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line 52,in load
2020-09-10T03:48:30.075254+00:00 app[web.1]: return self.load_wsgiapp()
2020-09-10T03:48:30.075254+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line 41,in load_wsgiapp
2020-09-10T03:48:30.075255+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-09-10T03:48:30.075255+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py",line 350,in import_app
2020-09-10T03:48:30.075255+00:00 app[web.1]: __import__(module)
2020-09-10T03:48:30.075255+00:00 app[web.1]: File "/app/spotlit_due_diligence/spotlit_due_diligence/wsgi.py",line 33,in <module>
2020-09-10T03:48:30.075256+00:00 app[web.1]: application = get_wsgi_application()
2020-09-10T03:48:30.075256+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/wsgi.py",line 14,in get_wsgi_application
2020-09-10T03:48:30.075256+00:00 app[web.1]: django.setup()
2020-09-10T03:48:30.075256+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/__init__.py",line 17,in setup
2020-09-10T03:48:30.075256+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG,settings.LOGGING)
2020-09-10T03:48:30.075257+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py",line 48,in __getattr__
2020-09-10T03:48:30.075257+00:00 app[web.1]: self._setup(name)
2020-09-10T03:48:30.075257+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py",line 44,in _setup
2020-09-10T03:48:30.075257+00:00 app[web.1]: self._wrapped = Settings(settings_module)
2020-09-10T03:48:30.075257+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py",line 92,in __init__
2020-09-10T03:48:30.075258+00:00 app[web.1]: mod = importlib.import_module(self.SETTINGS_MODULE)
2020-09-10T03:48:30.075258+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/importlib/__init__.py",line 37,in import_module
2020-09-10T03:48:30.075258+00:00 app[web.1]: __import__(name)
2020-09-10T03:48:30.075258+00:00 app[web.1]: File "/app/spotlit_due_diligence/spotlit_due_diligence/settings/powell.py",line 93,in <module>
2020-09-10T03:48:30.075259+00:00 app[web.1]: os.mkdir('encryption_keys')
2020-09-10T03:48:30.075259+00:00 app[web.1]: OSError: [Errno 17] File exists: 'encryption_keys'
2020-09-10T03:48:30.075406+00:00 app[web.1]: [2020-09-10 03:48:30 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-09-10T03:48:30.455830+00:00 app[web.1]: In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/syslimits.h:7:0,2020-09-10T03:48:30.455837+00:00 app[web.1]: from /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/limits.h:34,2020-09-10T03:48:30.455838+00:00 app[web.1]: from /app/.heroku/python/include/python2.7/Python.h:19,2020-09-10T03:48:30.455839+00:00 app[web.1]: from .heroku/python/lib/python2.7/site-packages/magic/__pycache__/_cffi__x14a33f6ax54cebdac.c:2:
2020-09-10T03:48:30.455839+00:00 app[web.1]: /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/limits.h:194:15: fatal error: limits.h: No such file or directory
2020-09-10T03:48:30.455840+00:00 app[web.1]: #include_next <limits.h>  /* recurse down to the real one */
2020-09-10T03:48:30.455841+00:00 app[web.1]: ^~~~~~~~~~
2020-09-10T03:48:30.455847+00:00 app[web.1]: compilation terminated.
2020-09-10T03:48:30.460417+00:00 app[web.1]: [2020-09-09 22:48:30 +0000] [13] [ERROR] Exception in worker process
2020-09-10T03:48:30.460418+00:00 app[web.1]: Traceback (most recent call last):
2020-09-10T03:48:30.460419+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",in spawn_worker
2020-09-10T03:48:30.460420+00:00 app[web.1]: worker.init_process()
2020-09-10T03:48:30.460420+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py",in init_process
2020-09-10T03:48:30.460420+00:00 app[web.1]: self.load_wsgi()
2020-09-10T03:48:30.460421+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py",in load_wsgi
2020-09-10T03:48:30.460421+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-09-10T03:48:30.460421+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py",in wsgi
2020-09-10T03:48:30.460422+00:00 app[web.1]: self.callable = self.load()
2020-09-10T03:48:30.460437+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",in load
2020-09-10T03:48:30.460437+00:00 app[web.1]: return self.load_wsgiapp()
2020-09-10T03:48:30.460438+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",in load_wsgiapp
2020-09-10T03:48:30.460438+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-09-10T03:48:30.460438+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py",in import_app
2020-09-10T03:48:30.460439+00:00 app[web.1]: __import__(module)
2020-09-10T03:48:30.460440+00:00 app[web.1]: File "/app/spotlit_due_diligence/spotlit_due_diligence/wsgi.py",in <module>
2020-09-10T03:48:30.460440+00:00 app[web.1]: application = get_wsgi_application()
2020-09-10T03:48:30.460440+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/wsgi.py",in get_wsgi_application
2020-09-10T03:48:30.460441+00:00 app[web.1]: django.setup()
2020-09-10T03:48:30.460441+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/__init__.py",line 18,in setup
2020-09-10T03:48:30.460441+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)
2020-09-10T03:48:30.460442+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/registry.py",line 85,in populate
2020-09-10T03:48:30.460442+00:00 app[web.1]: app_config = AppConfig.create(entry)
2020-09-10T03:48:30.460442+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/config.py",line 86,in create
2020-09-10T03:48:30.460443+00:00 app[web.1]: module = import_module(entry)
2020-09-10T03:48:30.460444+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/importlib/__init__.py",in import_module
2020-09-10T03:48:30.460444+00:00 app[web.1]: __import__(name)
2020-09-10T03:48:30.460444+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/magic/__init__.py",line 1,in <module>
2020-09-10T03:48:30.460445+00:00 app[web.1]: from . import ffi
2020-09-10T03:48:30.460445+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/magic/ffi.py",line 27,in <module>
2020-09-10T03:48:30.460446+00:00 app[web.1]: ext_package="magic")
2020-09-10T03:48:30.460446+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/cffi/api.py",line 437,in verify
2020-09-10T03:48:30.460447+00:00 app[web.1]: lib = self.verifier.load_library()
2020-09-10T03:48:30.460447+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/cffi/verifier.py",line 113,in load_library
2020-09-10T03:48:30.460447+00:00 app[web.1]: self._compile_module()
2020-09-10T03:48:30.460448+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/cffi/verifier.py",line 210,in _compile_module
2020-09-10T03:48:30.460448+00:00 app[web.1]: outputfilename = ffiplatform.compile(tmpdir,self.get_extension())
2020-09-10T03:48:30.460448+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/cffi/ffiplatform.py",line 29,in compile
2020-09-10T03:48:30.460449+00:00 app[web.1]: outputfilename = _build(tmpdir,ext,compiler_verbose)
2020-09-10T03:48:30.460449+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/cffi/ffiplatform.py",line 62,in _build
2020-09-10T03:48:30.460450+00:00 app[web.1]: raise VerificationError('%s: %s' % (e.__class__.__name__,e))
2020-09-10T03:48:30.460450+00:00 app[web.1]: VerificationError: CompileError: command 'gcc' failed with exit status 1
2020-09-10T03:48:30.460742+00:00 app[web.1]: [2020-09-09 22:48:30 +0000] [13] [INFO] Worker exiting (pid: 13)
2020-09-10T03:48:30.512245+00:00 app[web.1]: Traceback (most recent call last):
2020-09-10T03:48:30.512247+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn",line 8,in <module>
2020-09-10T03:48:30.512302+00:00 app[web.1]: sys.exit(run())
2020-09-10T03:48:30.512325+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line 61,in run
2020-09-10T03:48:30.512364+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2020-09-10T03:48:30.512385+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py",line 223,in run
2020-09-10T03:48:30.512517+00:00 app[web.1]: super(Application,self).run()
2020-09-10T03:48:30.512521+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py",line 72,in run
2020-09-10T03:48:30.512562+00:00 app[web.1]: Arbiter(self).run()
2020-09-10T03:48:30.512565+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",line 232,in run
2020-09-10T03:48:30.512633+00:00 app[web.1]: self.halt(reason=inst.reason,exit_status=inst.exit_status)
2020-09-10T03:48:30.512637+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",line 345,in halt
2020-09-10T03:48:30.512724+00:00 app[web.1]: self.stop()
2020-09-10T03:48:30.512724+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",line 396,in stop
2020-09-10T03:48:30.512823+00:00 app[web.1]: time.sleep(0.1)
2020-09-10T03:48:30.512824+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",line 245,in handle_chld
2020-09-10T03:48:30.512888+00:00 app[web.1]: self.reap_workers()
2020-09-10T03:48:30.512891+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py",line 528,in reap_workers
2020-09-10T03:48:30.512992+00:00 app[web.1]: raise HaltServer(reason,self.WORKER_BOOT_ERROR)
2020-09-10T03:48:30.513066+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2020-09-10T03:48:30.580846+00:00 heroku[web.1]: Process exited with status 1
2020-09-10T03:48:30.625801+00:00 heroku[web.1]: State changed from up to crashed

通常我觉得我可以读懂错误并进行分类,但是我什至不知道从哪里开始。

这是我的procfile:

web: gunicorn spotlit_due_diligence.spotlit_due_diligence.wsgi:application

这是我的wsgi.py文件:

import os
from os.path import abspath,dirname
from sys import path

SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT)

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this,use
# mod_wsgi daemon mode with each site in its own daemon process,or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "jajaja.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE","settings.powell")
# from whitenoise.django import DjangoWhiteNoise
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server,if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

感谢您提供的任何指导!

解决方法

2020-09-10T03:48:30.075258+00:00 app[web.1]: File "/app/spotlit_due_diligence/spotlit_due_diligence/settings/powell.py",line 93,in <module>
2020-09-10T03:48:30.075259+00:00 app[web.1]: os.mkdir('encryption_keys')
2020-09-10T03:48:30.075259+00:00 app[web.1]: OSError: [Errno 17] File exists: 'encryption_keys'

独角兽开始了多名工人。在您的代码spotlit_due_diligence/spotlit_due_diligence/settings/powell.py第93行中,使用os.mkdir('encryption_keys')将文件夹创建为文件夹。
在第一个工作程序运行时运行良好,但在第二个工作程序运行时将抛出错误。

您将必须使代码更健壮并处理该异常。也不是每个项目都与gunicorn兼容。
您是否也在使用gunicorn在本地运行项目?您通常将其最后包裹在金枪鱼中。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...