Delay on restart
I have a Flask app where is my main.py
from king_slayer.database import init_db, manager
from king_slayer.views import fetch_production
if __name__ == '__main__':
init_db()
manager.run()
Before doing, manger.run()
I want to call
fetch_production()
so i did it.
from king_slayer.database import init_db, manager
from king_slayer.views import fetch_production
if __name__ == '__main__':
init_db()
with manager.app.test_request_context():
fetch_production()
manager.run()
It works.
The problem is that I kill the application Flask
and then restart it, it doesn't start in 60-120 seconds. No mistakes, nothing. The browser won't load anything. a simple Loading Problem page appears and after that it works fine unless I restart the app again Flask
.
If I delete
with manager.app.test_request_context():
fetch_production()
there is no delay.
PS
If I don't use with manager.app.test_request_context():
Flask it throws this error.
Traceback (most recent call last):
File "main.py", line 8, in <module>
fetch_production()
File "/home/jarvis/Development/kingslayer/king_slayer/king_slayer/views.py", line 156, in fetch_production
results={"msg": "Database Updated", }
File "/home/jarvis/Development/kingslayer/local/lib/python2.7/site-packages/flask/json.py", line 235, in jsonify
and not request.is_xhr:
File "/home/jarvis/Development/kingslayer/local/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/jarvis/Development/kingslayer/local/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
return self.__local()
File "/home/jarvis/Development/kingslayer/local/lib/python2.7/site-packages/flask/globals.py", line 20, in _lookup_req_object
raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context
What's happening?
PPS fetch_production()
does something like this
requests.get(config.SERVER, auth=HTTPBasicAuth(config.AUTH_USER, config.AUTH_PASSWORD))
performs several functions, functions defined in views.py
, and then creates a file .ini
in the file system. Then a is returned at the end return jsonify
.
requests.get(config.SERVER, auth=HTTPBasicAuth(config.AUTH_USER, config.AUTH_PASSWORD))
is actually very fast, and if fetch_production()
called manually, it takes no more than a few milliseconds.
source to share
No one has answered this question yet
Check out similar questions: