sqlalchemy wtih python ThreadPool and Flask



我有一个需要处理的图像数据库。 每个图像大约需要一分钟来处理。 我正在尝试使其多线程。 我在工作线程中将数据输入数据库时遇到问题。 由于某种原因,数据不会保存。

下面是代码的外观。

images = Images.query.all()
images_ = []
for image in images:
images_.append(image)
pool = ThreadPool()
pool.map(dostuff, (images_)) 
pool.close() 
pool.join()
def dostuff(image):
# do stuff
image.done = True
db.session.commit()

知道会发生什么吗?我也试图将会话传递到dostuff(image, session)但这也不起作用。 任何帮助不胜感激!

您是否将图像添加到会话?

def dostuff(image):
# do stuff
image.done = True
db.session.add(image)
db.session.commit()

最新更新