Search code examples
pythonflaskflask-restless

Flask + Restless fails to reload in debug mode


I'm writing my first Flask-Restless API, and came to a small issue that is annoying me a bit.

It works fine if I it is not in DEBUG MODE.

But if I set the debug mode to True, the application seems to break while trying to reload (but it happens when accessing any endpoint), and the reload doesn't work.

I came with this small example, so you can see that I can reproduce it even in very small applications:

import flask_sqlalchemy
import flask_restless
import flask

app = flask.Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = flask_sqlalchemy.SQLAlchemy(app)

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True, nullable=False)

db.create_all()

manager = flask_restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Person)

app.run()

Now, every time I try to access any endpoint of my application I get the following error:

127.0.0.1 - - [30/Nov/2017 09:39:59] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/<path>/app.py", line 46, in <module>
    app.run()
  File "/<myvenc/>devices/lib/python3.5/site-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/<myvenv>/devices/lib/python3.5/site-packages/werkzeug/serving.py", line 737, in run_simple
    reloader_type)
  File "/<myvenv>/devices/lib/python3.5/site-packages/werkzeug/_reloader.py", line 257, in run_with_reloader
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
  File "/usr/lib/python3.5/signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread

It is clear that it is happening because of the reloader in DEBUG mode, and as I said, it works fine if I set DEBUG to False, or even if I start it using app.run(use_reloader=False)

I tried to find out if Restless use some multithreading even while in DEBUG and if I can disable it, but couldn't find anything related.

Versions:

Python==3.5.2
Flask==0.12.2
Flask-Restless==0.17.0
Flask-SQLAlchemy==2.3.2
Werkzeug==0.12.2

OS: Linux ElementaryOS 0.4.1 (based on Ubuntu 16.04)

Any idea why is that happening and how can I fix it?

** UPDATE ** [SOLVED]

The problem was actually calling the app.run and then executing with flask run... stupid thing that seems to be spawning two different threads.


Solution

  • The problem was actually calling this in the code:

    app.run() 
    

    and then executing with flask run that seems to be spawning two different threads.