最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

【已解决】gunicorn运行Flask的app出错:gunicorn.errors.HaltServer HaltServer Worker failed to boot 3

Flask crifan 564浏览 0评论

折腾:

【已解决】把NLP部分的代码合并到当前Flask的app中

期间,去把NLP的代码放进来:

后,再去用gunicorn去运行Flask的app,结果直接报错:

➜  robotDemo gunicorn -c gunicorn_config.py app:app

currentRootPath=/Users/crifan/dev/dev_root/company/xxx/projects/robotDemo

flaskHost=0.0.0.0, flaskPort=32851

Traceback (most recent call last):

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 202, in run

    self.manage_workers()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 544, in manage_workers

    self.spawn_workers()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 612, in spawn_workers

    time.sleep(0.1 * random.random())

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 244, in handle_chld

    self.reap_workers()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 524, in reap_workers

    raise HaltServer(reason, self.WORKER_BOOT_ERROR)

gunicorn.errors.HaltServer: <HaltServer ‘Worker failed to boot.’ 3>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/bin/gunicorn", line 11, in <module>

    sys.exit(run())

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 74, in run

    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/app/base.py", line 203, in run

    super(Application, self).run()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/app/base.py", line 72, in run

    Arbiter(self).run()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 231, in run

    self.halt(reason=inst.reason, exit_status=inst.exit_status)

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 344, in halt

    self.stop()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 393, in stop

    time.sleep(0.1)

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 244, in handle_chld

    self.reap_workers()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 524, in reap_workers

    raise HaltServer(reason, self.WORKER_BOOT_ERROR)

gunicorn.errors.HaltServer: <HaltServer ‘Worker failed to boot.’ 3>

➜  robotDemo

看不出具体错误。

gunicorn.errors.HaltServer: <HaltServer ‘Worker failed to boot.’ 3>

python – gunicorn.errors.HaltServer: <HaltServer ‘Worker failed to boot.’ 3> django – Stack Overflow

去看对应的gunicorn的error的log:

➜  robotDemo cat logs/gunicorn_error.log

[2018-04-25 16:46:54 +0800] [6463] [INFO] Worker exiting (pid: 6463)

[2018-04-25 16:46:54 +0800] [6464] [ERROR] Exception in worker process

Traceback (most recent call last):

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker

    worker.init_process()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/workers/gthread.py", line 109, in init_process

    super(ThreadWorker, self).init_process()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process

    self.load_wsgi()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi

    self.wsgi = self.app.wsgi()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi

    self.callable = self.load()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load

    return self.load_wsgiapp()

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp

    return util.import_app(self.app_uri)

  File "/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app

    __import__(module)

  File "/Users/crifan/dev/dev_root/company/xxx/projects/robotDemo/app.py", line 28, in <module>

    from AccessData import AccessData

  File "ai/nlp/dialog/AccessData.py", line 1, in <module>

    from data.xlstomysql import SearchByKeyword, SearchSeries

  File "ai/nlp/dialog/data/xlstomysql.py", line 2, in <module>

    import pymysql

ModuleNotFoundError: No module named ‘pymysql’

[2018-04-25 16:46:54 +0800] [6464] [INFO] Worker exiting (pid: 6464)

➜  robotDemo cat logs/gunicorn_error.log

发现原因了:

此处没有找到pymysql

所以此处需要去安装pymysql

➜  robotDemo pipenv install pymysql

Installing pymysql…

Collecting pymysql

  Downloading https://files.pythonhosted.org/packages/e5/07/c0f249aa0b7b0517b5843eeab689b9ccc6a6bb0536fc9d95e65901e6f2ac/PyMySQL-0.8.0-py2.py3-none-any.whl (83kB)

Installing collected packages: pymysql

Successfully installed pymysql-0.8.0

Adding pymysql to Pipfile’s [packages]…

Pipfile.lock (cdbe9e) out of date, updating to (9ab51a)…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (9ab51a)!

Installing dependencies from Pipfile.lock (9ab51a)…

  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 14/14 —

然后再去试试

错误依旧。再去看看error的log中错误原因

ModuleNotFoundError: No module named ‘openpyxl’

再去安装openpyxl:

➜  robotDemo pipenv install openpyxl

Installing openpyxl…

Collecting openpyxl

  Downloading https://files.pythonhosted.org/packages/dc/99/9c58d83d7f093c0af5f90875f8595d2e9587fc36532a8bb347608cf0876b/openpyxl-2.5.3.tar.gz (170kB)

Collecting jdcal (from openpyxl)

  Downloading https://files.pythonhosted.org/packages/a0/38/dcf83532480f25284f3ef13f8ed63e03c58a65c9d3ba2a6a894ed9497207/jdcal-1.4-py2.py3-none-any.whl

Collecting et_xmlfile (from openpyxl)

  Downloading https://files.pythonhosted.org/packages/22/28/a99c42aea746e18382ad9fb36f64c1c1f04216f41797f2f0fa567da11388/et_xmlfile-1.0.1.tar.gz

Building wheels for collected packages: openpyxl, et-xmlfile

  Running setup.py bdist_wheel for openpyxl: started

  Running setup.py bdist_wheel for openpyxl: finished with status ‘done’

  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/11/7d/47/3dad56b5d260c790d9110623ba66783a2ad345eb76dd63003b

  Running setup.py bdist_wheel for et-xmlfile: started

  Running setup.py bdist_wheel for et-xmlfile: finished with status ‘done’

  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/2a/77/35/0da0965a057698121fc7d8c5a7a9955cdbfb3cc4e2423cad39

Successfully built openpyxl et-xmlfile

Installing collected packages: jdcal, et-xmlfile, openpyxl

Successfully installed et-xmlfile-1.0.1 jdcal-1.4 openpyxl-2.5.3

Adding openpyxl to Pipfile’s [packages]…

Pipfile.lock (9ab51a) out of date, updating to (05d76c)…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (05d76c)!

Installing dependencies from Pipfile.lock (05d76c)…

  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 17/17 —

继续试试gunicorn

  File "/Users/crifan/dev/dev_root/company/xxx/projects/robotDemo/app.py", line 29, in <module>

    from GenerateResponse import GenerateResponse

  File "ai/nlp/dialog/GenerateResponse.py", line 3, in <module>

    story_responses = [item.split(‘\n’) for item in open("data/reply.txt").read().split(‘\n\n’)]

FileNotFoundError: [Errno 2] No such file or directory: ‘data/reply.txt’

很明显,是导入问题。

去看看代码,如何修改。

改为:

import random

import os

curPath = os.getcwd()

replyTextFullPath = os.path.join(curPath, "data/reply.txt")

#story_responses = [item.split(‘\n’) for item in open("data/reply.txt").read().split(‘\n\n’)]

story_responses = [item.split(‘\n’) for item in open(replyTextFullPath).read().split(‘\n\n’)]

结果:

还是出错:

FileNotFoundError: [Errno 2] No such file or directory: ‘/Users/crifan/dev/dev_root/company/xxx/projects/robotDemo/data/reply.txt’

看来:

此处的

os.getcwd()

只是获取到

当前python入口文件app.py被执行到的路径

而不是此处文件:

ai/nlp/dialog/GenerateResponse.py

被执行时候的路径

再去改为:

curProjectRootPath = os.getcwd()

curFilePath = os.path.join(curProjectRootPath, "ai/nlp/dialog")

replyTextFullPath = os.path.join(curFilePath, "data/reply.txt")

#story_responses = [item.split(‘\n’) for item in open("data/reply.txt").read().split(‘\n\n’)]

story_responses = [item.split(‘\n’) for item in open(replyTextFullPath).read().split(‘\n\n’)]

结果:

可以了。

至少Flask可以跑起来了:

【总结】

此处是用gunicorn去运行Flask的app,由于flask的app内部新增代码中,导入的库或文件出错,导致启动失败,报错:

gunicorn.errors.HaltServer: <HaltServer ‘Worker failed to boot.’ 3>

解决办法:

通过gunicorn的配置文件:

gunicorn_config.py

中的:

currentRootPath = os.getcwd()

print("currentRootPath=%s" % currentRootPath)

accesslog = currentRootPath + "/logs/gunicorn_access.log"      #访问日志文件

去找gunicorn的error的log文件:

logs/gunicorn_error.log

然后看到详细错误信息

分别是:

  • 缺少库:pymysql

  • 缺少库:openpyxl

  • 内部代码导入文件出错

则分别去:

  • pipenv install pymysql

  • pipenv install openpyxl

  • 修改导入路径,使得可以找到文件

import os

curProjectRootPath = os.getcwd()

curFilePath = os.path.join(curProjectRootPath, "ai/nlp/dialog")

replyTextFullPath = os.path.join(curFilePath, "data/reply.txt")

#story_responses = [item.split(‘\n’) for item in open("data/reply.txt").read().split(‘\n\n’)]

story_responses = [item.split(‘\n’) for item in open(replyTextFullPath).read().split(‘\n\n’)]

最终正常启动了Flask的app。

转载请注明:在路上 » 【已解决】gunicorn运行Flask的app出错:gunicorn.errors.HaltServer HaltServer Worker failed to boot 3

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
65 queries in 0.121 seconds, using 9.50MB memory