我有一个非常简单的FLASK网络服务器,我在heroku平台的网络dyno中运行。我还有一个非常简单的python程序,它运行在一个单独的dyno上,它打印"hello world"然后退出。
我的程序文件看起来像这样
web: gunicorn --bind 0.0.0.0:$PORT wsgi
test: python helloWorld.py
使用heroku本地,一旦hello world程序完成,网络dyno也会被杀死:
$ heroku local
8:54:29 AM test.1 | Hello World
[DONE] Killing all processes with signal SIGINT
8:54:29 AM test.1 Exited Successfully
8:54:29 AM web.1 | Traceback (most recent call last):
8:54:29 AM web.1 | File "/anaconda3/bin/gunicorn", line 7, in <module>
8:54:29 AM web.1 | from gunicorn.app.wsgiapp import run
8:54:29 AM web.1 | File "/anaconda3/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 9, in <module>
8:54:29 AM web.1 | from gunicorn.app.base import Application
8:54:29 AM web.1 | File "/anaconda3/lib/python3.7/site-packages/gunicorn/app/base.py", line 11, in <module>
8:54:29 AM web.1 | from gunicorn._compat import execfile_
8:54:29 AM web.1 | File "/anaconda3/lib/python3.7/site-packages/gunicorn/_compat.py", line 267, in <module>
8:54:29 AM web.1 | import inspect
8:54:29 AM web.1 | File "/anaconda3/lib/python3.7/inspect.py", line 1087, in <module>
8:54:29 AM web.1 | 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
8:54:29 AM web.1 | File "/anaconda3/lib/python3.7/collections/__init__.py", line 397, in namedtuple
8:54:29 AM web.1 | exec(s, namespace)
8:54:29 AM web.1 | File "<string>", line 1, in <module>
8:54:29 AM web.1 | KeyboardInterrupt
8:54:29 AM web.1 Exited with exit code null
我该如何防止这种情况?
我知道问题出在 helloWorld.py 退出。如果我创建的脚本不会终止,则不会出现问题。
print("Hello World", flush=True)
while True:
pass
显然这是工头的故意行为。请参阅Github上的讨论:
福尔曼假设所有的过程都是长期存在的。如果任何进程退出,Foreman 将停止所有进程。
唯一的解决办法就是让 HelloWorld.py 长寿。