Flask stuck, no response of

Their learning Flask + Gevent, made a small interface server, but after receiving the request, the print request message, and returns the correct format, will be after receiving the request message, Flask stuck unresponsive after running problems, sometimes clicking ctrl + C in order to continue implementation of the internet to find a lot of methods, but in the end found inspiration from http.server this framework, because http.server in while getting the message, it will also appear stuck, or processing wait 30s in response to the above, for Flask, I also used the same idea, really fast speed, not the card and unresponsive.

code show as below:

@app.route('/alert', methods=['POST'])
def req_alert():
    print('---------------------')
    print(request.headers)
    if request.data == b'':
        print('请求数据格式为空!')
        abort(401)
    if request.is_json:
        req_str = request.data[0:int(request.content_length)]
        req_data = json.loads(req_str, encoding='. 8-UTF ' ) 
        print_req_data (REQ_DATA) 
    the else :
         Print ( ' request data format is not the format Json ' ) 
        ABORT ( 401 )
     Print ( ' --------------------- \ n- ' ) 

    headers = Request.Headers
     the try :
         IF REQ_DATA [ ' Action ' ] =! ' Alert '  and REQ_DATA [ ' Action ' ] =! ' alert_release '  :
            ABORT (401, 'Invalid action')

        if headers['message-type'] != 'alert':
            abort(401, 'Invalid message-type')

        alert_id = req_data['alert_id']

        res_data = {
            'result_code': '1',
            'result_desc': 'Success',
            'timestamp': int(time.time()),
            'data': {'alert_id': alert_id}
        }
        return res_data

    except KeyError:
        abort(401, 'Invalid req data')

    except Exception as e:
        abort(401, 'Invalid req data')

 

Focus on getting the message is to get request.content_length,. So speed up the processing speed in through the interception effective message length information directly from request.data length can be, and no longer stuck phenomenon. Why direct call request case .data, or request.get_json or otherwise there will be jammed, I specifically did not study, I personally suspect that the message information requested in addition to the effective length of the content, as well as other information can lead to occupy the cache write superficial also please the god pointing.

Guess you like

Origin www.cnblogs.com/howardwu/p/11237373.html