在 Flask 应用程序中线程化外部脚本

Threading an external script within a Flask app

我有一个 Flask 应用程序正在使用外部脚本来执行某些操作。在其中一个脚本中,我使用 threading 到 运行 线程。

我正在使用以下代码进行实际线程处理:

for a_device in get_devices:
    my_thread = threading.Thread(target=DMCA.do_connect, args=(self, a_device, cmd))
    my_thread.start()

main_thread = threading.currentThread()
for some_thread in threading.enumerate():
    if some_thread != main_thread:
        some_thread.join()

但是,当此脚本获取 运行(来自表单)时,进程将挂起,我将在网页上获得连续的加载循环。
有没有其他方法可以在应用程序中使用多线程?

我自己在 Flask 应用程序中实现线程对我来说总是以某种灾难告终。您可能想要使用分布式任务队列,例如 Celery。尽管可能很想自己分拆线程以更快地完成它,但在此过程中您将开始面临各种问题,并最终浪费大量时间(恕我直言)。

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.

The execution units, called tasks, are executed concurrently on a single or more worker servers using multiprocessing, Eventlet, or gevent. Tasks can execute asynchronously (in the background) or synchronously (wait until ready).

这里有一些您可以用来入门的好资源