Flask python app failing to get app object after initializing -
app_instance.py
from app import flaskapp app = none def init_instance(env): global app app = flaskapp(env) def get_instance(): assert app not none return app flaskapp class pretty this
class flaskapp(object): def __init__(self, env): self.oauth_manager = .... bla bla .. self.clients_manager = .. bla bla .. app = flask(__name__) app.config.from_object(env) app = app_wrapper.wrap(app, app.config['num_proxy_servers']) self.app = app self.api = api(self.app, prefix='/v3', default_mediatype='application/json') self.define_routes() # initialize db self.db = database(self.app) fmt = "%(asctime)s - %(request_id)s - %(name)s - %(levelname)s - %(message)s" logging.basicconfig(format=fmt, level=self.app.config.get('log_level')) request_id.init(app, prefix='my_api_', internal=false) def run_server(self): self.app.run(host=self.app.config['host'], port=self.app.config['port'], debug=self.app.config['debug']) def define_routes(self): # configure api resources self.api.add_resource(versionlistcontroller, '/my/route', endpoint='versions') more routes here self.api.init_app(self.app) in app controller
def is_valid_oauth_token(request): mobile_module import app_instance app = app_instance.get_instance() # more code here i'm running app on localhost , getting
assert app not none assertionerror how can "fix" code? should importing from mobile_module import app_instance in every route access? suggestions please
i should state app works in production behind nginx
i guess question more python (how make work) , less in flask.
the problem not related get_instance or init_instance(create_app etc.).
flask has different states. app work in out of request context when initialize app instance(flaskapp(env)).
as see in example, try application in context of request(def is_valid_oauth_token(request)). means not initialization of application. processing while request active. other state of application - app created , work in context of request. in case can application instance using from flask import current_app.
to better understanding how works/use recommend read flask._app_ctx_stack, app_context() , flask.g.
hope helps.
Comments
Post a Comment