简体   繁体   English

Python 期货并发在 Flask 中不起作用

[英]Python Futures Concurrent not working in Flask

I am using Python Futures Concurrent inside one of the method for getting the result in python, it is working fine when I tried it on individual file, but when I integrate it with Flask Api, the result is undefined, I debug and find out that the flask api is returning the result without even waiting to the futures to finish processing the result. I am using Python Futures Concurrent inside one of the method for getting the result in python, it is working fine when I tried it on individual file, but when I integrate it with Flask Api, the result is undefined, I debug and find out that flask api 正在返回结果,甚至没有等待期货完成处理结果。 How to stop it till the result is processed.如何停止它直到处理结果。

My Code of futures inside the function which is called by the flask api:我在 function 中的期货代码由 flask api 调用:

with concurrent.futures.ThreadPoolExecutor() as executor:
    futures_np1 = [executor.submit(self.get_test_score, data, param) for param in param_list]
    futures_np2 = [executor.submit(self.get_test_score, param, data) for param in param_list]


result1 = [f.result() for f in futures_np1]
result2 = [f.result() for f in futures_np2]
return self.get_average_score(result1, result2)

Can anyone give better alternative for multithreading to speed up the processing according to the given problem?谁能根据给定的问题为多线程提供更好的替代方案以加快处理速度?

if anyone looking for the answer.如果有人在寻找答案。 The problem arrises because I am passing multiple parameters to futures which is returning a list of result, that will work perfectly on plain python, but in flask it will not work, so to work it out you have to only submit one request at time by using loop.出现问题是因为我将多个参数传递给返回结果列表的期货,这将在普通 python 上完美运行,但在 flask 中它将不起作用,所以要解决这个问题,您只需一次提交一个请求使用循环。 Example:例子:

with concurrent.futures.ThreadPoolExecutor() as executor:
    for param in param_list:
        futures_np1 = executor.submit(self.get_test_score, data, param)
        result1.append(futures_np1.result())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM