0

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.

ThangTu
  • 41
  • 1
  • 9
  • Thanks a lot to @miguelgrinberg. Add `db.session.remove()` at the end of the loop solved the problem. Detail: https://stackoverflow.com/questions/42347875/why-flask-background-thread-get-wrong-database-information – ThangTu Sep 12 '22 at 16:37

0 Answers0