Flask throwing 'working outside of request context' when starting sub thread

后端 未结 5 1752
遇见更好的自我
遇见更好的自我 2020-11-30 21:23

I am trying to start a new thread in Python inside of a Flask application. I am doing background work that gets triggered by the request, but I don\'t need to wait for the

5条回答
  •  萌比男神i
    2020-11-30 21:55

    You can copy the desired info and pass it on:

    @app.route('/my_endpoint', methods=['POST'])
    def my_endpoint_handler():
        #do tracking in sub-thread so we don't hold up the page
        def handle_sub_view(data):
            # Use the data in subprocess
        data = request.get_json()  # copy the data
        thread.start_new_thread(handle_sub_view, data)
        return "Thanks"
    

提交回复
热议问题