用Python实现Flask多线程

用Python实现Flask多线程,flask,Flask,我只是想知道是否有可能启动两个功能,以便与Flask服务器同时工作?我需要在触发函数_2后启动函数_1,并同时运行这两个函数。可能吗 def function_1(): yield "start_function_2" counter = 0 while True: counter += 1 print counter def function_2(): second_counter = 0 while True:

我只是想知道是否有可能启动两个功能,以便与Flask服务器同时工作?我需要在触发
函数_2
后启动
函数_1
,并同时运行这两个函数。可能吗

def function_1():
    yield "start_function_2"
    counter = 0
    while True:
       counter += 1
       print counter


def function_2():
    second_counter = 0
    while True:
       second_counter += 1
       print second_counter

def main():
    return render_template("index.html")

@app.route("/start_functions", methods=["POST"])
def start_functions():
    data = request.data
    if request.method == "POST":
        for i in function_1(data):
           if (i == "start_function_2"):
              function_2()

if __name__ == "__main__":
    app.run(dhost="0.0.0.0", port=port)

是的,你可以。您只需要导入
线程
模块

import threading

def function1():
    # your code here

def function2():
   # your code here
您可以这样启动线程:

threading.Thread(target=function1).start()
threading.Thread(target=function2).start()
两者将同时存在


你可以找到很好的教程来进一步解释:)

这是最简洁、最有用的答案。