本文主要是介绍多进程池Flask实战应用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
多进程池Flask实战应用
import json
import math
import flask
from concurrent.futures import ProcessPoolExecutor
app = flask.Flask(__name__)
process_pool = ProcessPoolExecutor()
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n+1, 2):
if n % i == 0:
return False
return True
@app.route("/is_prime/<number>")
def api_is_prime(number):
number_list = [int(x) for x in number.split(",")]
result = process_pool.map(is_prime, number_list)
return json.dumps(dict(zip(number_list, result)))
if __name__ == '__main__':
app.run()
这篇关于多进程池Flask实战应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!