Forums

Creating app context for scheduled task

I would like to query the Flask app's database and utilize preexisting functions to generate and send a report. I can't access the current_app and trying to import any module returns a relative import error. Is creating an another instance of the app what I need to do? Any suggestions?

You are trying to create a scheduled task to generate a send a report? From my understanding, the current_app context is created on the fly every time flask receives and is processing a request.

If you are just trying to query the db maybe just use sqlalchemy?

I recently and coincidentally encountered this type of import error with my app (and not just scheduled tasks) when trying to access the global configurations.

I have a manage.py which generates an instance of app. In a seperate module I can do the following...

import manage
print manage.app.config

and a dictionary with my app's configurations will be printed out in the shell. However if I try to use the configuration's in my module like so

RANDOM_NAME = manage.app.config['RANDOM_NAME']

My app will crash. The stack trace is below:

2015-11-04 18:10:24,682 :/usr/lib/python2.7/threading.py:1160:

RuntimeWarning: tp_compare didn't return -1 or -2 for exception
2015-11-04 18:10:24,682 :  return _active[_get_ident()]
2015-11-04 18:10:24,682 :Traceback (most recent call last):
2015-11-04 18:10:24,682 :  File "/bin/user_wsgi_wrapper.py", line 134, in __call__
2015-11-04 18:10:24,683 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-11-04 18:10:24,683 :  File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-11-04 18:10:24,683 :    self.error(msg, *args, **kwargs)
2015-11-04 18:10:24,683 :  File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-11-04 18:10:24,683 :    self._log(ERROR, msg, args, **kwargs)
2015-11-04 18:10:24,683 :  File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-11-04 18:10:24,684 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-11-04 18:10:24,684 :  File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-11-04 18:10:24,684 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-11-04 18:10:24,684 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-11-04 18:10:24,684 :    self.threadName = threading.current_thread().name
2015-11-04 18:10:24,684 :  File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-11-04 18:10:24,685 :    return _active[_get_ident()]
2015-11-04 18:10:24,685 :  File "/bin/user_wsgi_wrapper.py", line 126, in __call__
2015-11-04 18:10:24,685 :    app_iterator = self.app(environ, start_response)
2015-11-04 18:10:24,685 :  File "/bin/user_wsgi_wrapper.py", line 140, in import_error_application
2015-11-04 18:10:24,685 :    raise e
2015-11-04 18:10:24,685 :ImportError: cannot import name app

Again, I don't run into these problems on my local machine when using the build in Flask server. Any hints?

Debugging import errors from our help pages

I'll have another look at this. Thanks.