首页 > 解决方案 > Flask 脚本作为切换服务

问题描述

我有一个永远运行循环的函数:

def some_function():
    while True:
         #some tasks here

我需要编写一个 Flask 脚本,这样当请求到来时,如果该函数停止,则应该开始执行该函数,反之亦然。因此,例如,对于这样的函数:

def some_function():
    count = 0
    while True:
         print(count)
         sleep(5)

我需要这样的东西:

@app.route('/')
def toggle():
    if function_isnt_running():
        start_function_execution()
        return Response('Started')
    else:
        stop_function_execution()
        return Response('Stopped')

标签: pythonflask

解决方案


推荐阅读