3

I have developed a REST server using Flask in python and currently it is built in synchronous manner. next request is processed only after completion of existing request and sometimes this is increasing response time. Most of the processing is network dependent and takes few seconds. What are the best ways to handle ie fork so that I can handle multiple requests simultaneously.

@app.route('call', methods = ['POST'])
def create_task2():
    result = process(request) # takes around 5 seconds
    return jsonify( result  ), 201

When 2 request are sent to my restserver simultaneously, 2nd one has to wait for atleast 10 seconds before receiving the response. I want to use this as a restserver catering requests to external users

4

1 回答 1

3

发生这种情况只是因为您使用的是包含的开发服务器。Flask 是一个 Web 框架,而不是 Web 服务器。

并发请求服务是由网络服务器处理的任务。

你可以使用像 uWSGI 这样的 wsgi 网络服务器来为你的烧瓶应用程序提供服务。为了获得更高的性能,您还可以将静态服务委托给 NGINX,但对于纯 REST 服务器,通常不需要。

使用 uWSGI,您可以指定并行处理的工作人员(进程)的数量。

请记住,处理大量请求并没有什么神奇之处。即使您使用更多的进程或线程,您也必须处理该数量的并发请求。

于 2013-09-08T11:56:17.683 回答