多进程池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()
上一篇:欧拉筛与素数判定


下一篇:【POJ】2739