Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Flask中的新线程中启动无限python脚本_Python_Multithreading_Flask_Process_Daemon - Fatal编程技术网

在Flask中的新线程中启动无限python脚本

在Flask中的新线程中启动无限python脚本,python,multithreading,flask,process,daemon,Python,Multithreading,Flask,Process,Daemon,我想从Flask请求中开始我的永无止境的Python脚本: def start_process(): exec(open("./process/main.py").read(), globals()) print("Started") return 并应要求: @app.route("/start") def start(): from threading import Thread thread = Thread(target=start_proce

我想从Flask请求中开始我的永无止境的Python脚本:

def start_process():
    exec(open("./process/main.py").read(), globals())
    print("Started")
    return
并应要求:

@app.route("/start")
   def start():
   from threading import Thread
   thread = Thread(target=start_process, args=())
   thread.setDaemon(True)
   thread.start()
   return redirect(url_for('main'))
main.py
进程是一个小型测试服务器,它等待一些消息,但它只是挂起整个flask脚本(事实上,通过gunicorn,如果我发送CTRL-C,我可以看到子进程的输出)


如何使
main.py
脚本单独启动?

我还没有成功地从Flask中启动长时间运行的线程,但却采用了另一种方式:在线程中启动Flask,并为它提供与其他线程通信的方式

诀窍是按照

def webserver(coordinator):
    app.config['COORDINATOR'] = coordinator
    app.run(use_reloader=False)
    # use_reloader=False is needed to keep Flask happy in a thread

def main():
    coordinator = Coordinator()
    ui = threading.Thread(target=webserver, args=(coordinator,))
    ui.start()
    # start other threads, passing them coordinator
    # start long-running tasks in main thread, passing it coordinator

其中,
协调器
使用
线程
功能(例如,
队列
)来保护对共享数据或资源的访问。您可以相当轻松地设置协调器以支持工作线程阻塞,直到发出信号(从处理程序发出),然后启动长时间运行的任务。

它与我想要做的相反,但它始终是一种解决方案。