在后台同时执行多个脚本

Execute several scripts at the same time in the background

我有一个页面,用户在其中选择一个 Python 脚本,然后执行该脚本。 我的问题是有些脚本需要一段时间才能执行(最多 30m)所以我想 运行 在后台运行它们,同时用户仍然可以在网站上导航。

我尝试使用 Celery 但因为我在 Windows 我不能比使用 --pool=solo 做得更好,虽然允许用户做其他事情,但只能这样做一次一个用户。

我在搜索解决方案时也看到了 this thread,但未能真正理解它的工作原理和实现方式,也无法确定它是否真的能解决我的问题...

所以这是我的问题:如何在 Celery 上同时在 Windows 上有多个 thread/multiple 进程?或者如果有其他方法,如何在后台同时执行多个任务?

您确定您的慢速脚本属于 CPU-bound 任务还是 I/O 绑定任务?

如果它们是I/O绑定的,您可以根据the blog from distributedpython.com

中的策略1使用eventlet和gevent

但如果它们是 CPU 绑定的,您可能不得不考虑使用像专用 Celery windows 盒子(或 windows Docker 容器)这样的方法根据 the blog from distributedpython.com

中的策略 2,通过设置环境变量 (FORKED_BY_MULTIPROCESSING=1) 来解决 Windows 上的 Celery billiard 问题