Python + Flash + Flash Sqlalchemy to realize MySQL database operation
Write test cases in Python to realize MySQL query
The test cases are as follows:
class MySQLTest(unittest.TestCase):
def test_queryAll(self):
uas = db.session.query(UserAgent).all()
self.assertIsNotNone(uas)
for ua in uas:
print("\n")
print(ua)
if __name__ == '__main__':
unittest.main()
1、 Problem description
Error message below
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
If no application is found, either work in the view function or push the application context
The first reaction should be related to writing test cases in Java Web applications, and the application context cannot be found
2、 Problem analysis
The test case starts a thread to execute database operation separately, which is independent of the flash application. It is unable to obtain the configuration information of the flash application context (including database connection information), so it is necessary to push the application context (inject context object)
This is why it works in the view function, which runs in the web container of flash.
3、 Solution
Python uses the with statement to manage context
The with statement is essentially context management
1. Context management protocol. Inclusion method__ enter__() And exit (), which support the protocol object to implement these two methods
2. The context manager defines the runtime context to be established when executing the with statement, and is responsible for executing the entry and exit operations in the context of the with statement block
3. Execute when entering the context__ enter__ Method. If the as var statement is set, the VaR variable accepts__ enter__() Method returns a value
4. If an exception occurs during runtime, exit the context manager. Call manager__ exit__ method.
Push the application context, that is, at the top where the thread starts executing code,
add with app.app_ context():
class MySQLTest(unittest.TestCase):
def test_queryAll(self):
with app.app_context():
uas = db.session.query(UserAgent).all()
self.assertIsNotNone(uas)
for ua in uas:
print("\n")
print(ua)
if __name__ == '__main__':
unittest.main()
4、 Result verification
Add with app.app_Context():
just write your own program logic, and the test case runs successfully
Read More:
- [Solved] Flask Application Context Error: RuntimeError: Working outside of application context.
- [Solved] Python Project Error: django.core.exceptions.ImproperlyConfigured: WSGI application ‘WebTool.wsgi.application
- [Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
- [Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation
- [Solved] Pyinstaller Error: “RuntimeError: No metadata path found for distribution ‘greenlet‘
- [Solved] pytorch loss.backward() Error: RuntimeError: Function AddBackward0 returned an invalid gradient at index 1…
- [Solved] RuntimeError: ProcessGroupNCCL is only supported with GPUs, no GPUs found
- Django WSGI protocol application, based on wsgiref module DIY a web framework
- [Solved] Python project runs the open() function error: FileNotFoundError: [Errno 2] No such file or directory
- [Solved] Using summary to View network parameters Error: RuntimeError: Input type (torch.cuda.FloatTensor)
- [Solved] RuntimeError: Numpy is not available (Associated Torch or Tensorflow)
- [Solved] RuntimeError: function ALSQPlusBackward returned a gradient different than None at position 3, but t
- The anaconda-navigator interface cannot be started, loading application, report ERROR download_api._get_url error
- [Solved] RuntimeError An attempt has been made to start a new process
- [Solved] Django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).
- RuntimeError: CUDA error: an illegal memory access was encountered
- [Solved] RuntimeError: one of the variables needed for gradient computation has been modified by an inplace
- [Solved] mindinsight modelart Error: RuntimeError: An attempt has been made to start a new process before…
- [Solved] RuntimeError: expected scalar type Long but found Float
- Pytorch Error: runtimeerror: expected scalar type double but found float