Supervisor not working with Gunicorn + Flask

2024/10/15 1:16:36

I am trying to run Gunicorn from Supervisor in an Ubuntu 12.04 system. Gunicorn runs a Flask app (simple REST web service tested with Flask's embedded server). I have installed Gunicorn by clonning GIT repo, trying to avoid 'apt-get install' because it runs Gunicorn server when installs it. I do not want it running, it will be run by Supervisor only.

So after install it, if I try:

cd /usr/local/bin
gunicorn my_app:app -c /path/to/gu_config_file

Gunicorn works. Then I kill it. Note config file without extension, because with '.py' extension does not work for me. So I edit Supervisor's config file like:

[program:gunicorn]
command=/usr/local/bin/gunicorn my_app:app -c /path/to/.gu_setup
directory=/usr/local/bin/
autostart=true
autorestart=true
redirect_stderr=True

And update changes in Supervisor:

supervisorctl reread
# gunicorn: changed
supervisorctl update
# gunicorn: stopped
# gunicorn: updated process group

Detects changes in file and works for Gunicorn program. Ok, but then I try to start it:

supervisorctl start gunicorn

Getting an annoying:

gunicorn: ERROR (abnormal termination)

Checking supervisor's log:

2013-03-08 13:07:22,378 INFO spawned: 'gunicorn' with pid 3355
2013-03-08 13:07:22,916 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:23,918 INFO spawned: 'gunicorn' with pid 3361
2013-03-08 13:07:24,492 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:26,496 INFO spawned: 'gunicorn' with pid 3367
2013-03-08 13:07:27,078 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:30,085 INFO spawned: 'gunicorn' with pid 3373
2013-03-08 13:07:30,628 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:31,630 INFO gave up: gunicorn entered FATAL state, too many start retries too quickly

I do not know what to do right now... Can you help me? Thx a lot!

EDIT: sorry I forgot to say I have exported PYTHONPATH variable as:

export PYTHONPATH=/usr/local/bin:/usr/local/lib/project

'my_app' is in /usr/local/bin. The lib path is needed for other modules. I have edited also Supervisor config file to indicate environmental variable, like:

environment=PYTHONPATH=/usr/local/bin:/usr/local/lib/project/

But did not work.

EDIT 2: as @robertklep suggest in his comment, this is log's output:

Traceback (most recent call last):File "/tmp/gunicorn/gunicorn/arbiter.py", line 485, in spawn_workerworker.init_process()File "/tmp/gunicorn/gunicorn/workers/base.py", line 100, in init_processself.wsgi = self.app.wsgi()File "/tmp/gunicorn/gunicorn/app/base.py", line 103, in wsgiself.callable = self.load()File "/tmp/gunicorn/gunicorn/app/wsgiapp.py", line 25, in loadreturn util.import_app(self.app_uri)File "/tmp/gunicorn/gunicorn/util.py", line 369, in import_app__import__(module)File "/usr/local/bin/my_app.py", line 4, in <module>import const
ImportError: No module named const
2013-03-08 13:29:35 [3670] [INFO] Worker exiting (pid: 3670)
2013-03-08 13:29:36 [3665] [INFO] Shutting down: Master
2013-03-08 13:29:36 [3665] [INFO] Reason: Worker failed to boot.

'const' module is in /usr/local/lib/project...

Answer

I don't see you setting the environment in your supervisor config file:

[program:gunicorn]
environment=PYTHONPATH=/usr/local/bin:/usr/local/lib/project
command=/usr/local/bin/gunicorn my_app:app -c /path/to/.gu_setup
...

If that doesn't work, try starting gunicorn in debug mode:

command=/usr/local/bin/gunicorn --debug --log-level debug my_app:app -c /path/to/.gu_setup

Or pass the path directly to gunicorn:

command=/usr/local/bin/gunicorn --pythonpath /usr/local/bin,/usr/local/lib/project my_app:app -c /path/to/.gu_setup

EDIT: gunicorn's --pythonpath is broken, you can only pass one directory:

command=/usr/local/bin/gunicorn --pythonpath /usr/local/lib/project my_app:app -c /path/to/.gu_setup
https://en.xdnf.cn/q/69341.html

Related Q&A

How to hash int/long using hashlib in Python?

Im developing a set of cryptographic algorithms / protocols for educational purposes. Specifically, I am currently working on OAEP encoding.OAEP involves use of cryptographic hash functions; therefore …

SQLAlchemy: Override relationship-defined order_by in a query

So, I have a model that is something like:class Foo(model):__tablename__ = "foo"id = Column(Integer, primary_key=True)data = relationship("FooData",cascade="all, delete-orphan&…

Toplevel in Tkinter: Prevent Two Windows from Opening

Say I have some simple code, like this:from Tkinter import * root = Tk() app = Toplevel(root) app.mainloop()This opens two windows: the Toplevel(root) window and the Tk() window. Is it possible to avoi…

Specify File path in tkinter File dialog

I have a file dialog to open a file, however, the file that I want to open is in a different directory than the program I wrote. The file dialog opens to the directory where I am. Is there a way to s…

Why does scipy linear interpolation run faster than nearest neighbor interpolation?

Ive written a routine that interpolates point data onto a regular grid. However, I find that scipys implementation of nearest neighbor interpolation performs almost twice as slow as the radial basis f…

How do I create a 404 page?

My application catches all url requests with an @app.route, but occasionally I bump into a bad url for which I have no matching jinja file (bu it does match an existing @app.route). So I want to redire…

Injecting pre-trained word2vec vectors into TensorFlow seq2seq

I was trying to inject pretrained word2vec vectors into existing tensorflow seq2seq model.Following this answer, I produced the following code. But it doesnt seem to improve performance as it should, a…

MySQL Stored Procedures, Pandas, and Use multi=True when executing multiple statements

Note - as MaxU suggested below, the problem is specific to mysql.connector and does not occur if you use pymysql. Hope this saves someone else some headachesUsing Python, Pandas, and mySQL and cannot…

How can I change the font size in GTK?

Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:lbl.set_markup("<span font_desc=Tahoma …

How to read BigQuery table using python pipeline code in GCP Dataflow

Could someone please share syntax to read/write bigquery table in a pipeline written in python for GCP Dataflow