How to get the most up-to-date database in app_context thread?

Viewed 21

This is the thread to track the status of the Order. If order status is True, doing some stuff and return True, end the thread.

def order_process(orderid):
    app = current_app._get_current_object()
    order_process_async_thread= threading.Thread(name='order_process_async', target=order_process_async, args=(app, orderid))
    order_process_async_thread.start()


def order_process_async(app, orderid):
    with app.app_context():
        while True:
            order = Order.query.filter_by(orderid=orderid).first()
            if order.status == True:
               # Doing some stuff
               return True
            time.sleep(1)

I run another thread to set the order status to True and commit the database successfully, but the database in the app_context is not up-to-date. The order status value in the app_context is still False and order_process_async thread never end. I think I'm missing some line of code to update the app_context but don't know what it is.

0 Answers
Related