一般的并行性
一般来说,如果您想并行运行任务,您可以使用线程或进程。在 python 中,线程非常适合 I/O 绑定的任务(意味着它们花费的时间用于等待另一个资源 - 等待您的数据库、磁盘或远程 Web 服务器),而进程非常适合任务受 CPU 限制(数学和其他计算密集型任务)。
concurrent.futures
在您的情况下,线程是理想的。 Python 有一个您可以查看的threading 模块,但有一点需要解压:安全使用线程通常意味着通过使用线程池和任务队列来限制可以运行的线程数。出于这个原因,我更喜欢 concurrent.futures 库,它提供了 threading 的包装器,为您提供易于使用的界面并为您处理很多复杂性。
使用concurrent.futures 时,您创建一个执行程序,然后将任务连同参数列表一起提交给它。而不是调用这样的函数:
# get 4 to the power of 5
result = pow(4, 5)
print(result)
您提交函数及其参数:
你通常会像这样使用 concurrent.futures:
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor()
future = executor.submit(pow, 4, 5)
print(future.result())
注意我们如何不使用pow()调用函数,我们提交函数对象pow,执行器将在线程内调用。
为了更轻松地将concurrent.futures 库与 Flask 一起使用,您可以使用 flask-executor,它与任何其他 Flask 扩展一样工作。它还处理后台任务需要在后台任务中访问 Flask 的上下文局部变量(如 app、session、g 或 request 对象)的边缘情况。完全披露:我编写并维护了这个库。
(有趣的事实:concurrent.futures 使用相同的 API 封装了线程和多处理 - 因此,如果您发现自己将来需要多处理来处理 CPU 绑定的任务,您可以以相同的方式使用相同的库来实现您的目标)
把它们放在一起
下面是使用 flask-executor 并行运行 SQLAlchemy 任务的样子:
from flask_executor import Executor
# ... define your `app` and `db` objects
executor = Executor(app)
# run the same query three times in parallel and collect all the results
futures = []
for i in range(3):
# note the lack of () after ".all", as we're passing the function object, not calling it ourselves
future = executor.submit(db.session.query(MyModel).all)
futures.append(future)
for future in futures:
print(future.result())
Boom,您现在已经并行运行了多个 Flask SQLAlchemy 查询。