Python:确保并发线程已完成



我正在尝试编写一个能与线程一起工作的python程序。我正在使用concurrent.futures来处理线程。在程序中,一个线程应该创建一个PDF文件。因为这个任务持续时间很长,所以我想创建一个线程来处理创建。但过了一段时间,我想再次使用pdf文件。因此,我必须确信,上一个线程已经完成。

我的问题是,我如何检查并发的未来线程是否完成,或者如何等待它的执行。

代码

if __name__ == '__main__'
with concurrent.futures.ThreadPoolExecutor() as executor:
document = executor.submit(createPDF, pdfValues)
#working on something different .... 
#Here I want to work with the pdf file again, via a new thread
#Therefore I want to make sure that the thread above is finished.
with concurrent.futures.ThreadPoolExecutor() as executor:
result = executor.submit(workWithPDF, values)

您的document变量的类型是concurrent.futures.Future,它有done()方法,如果它完成了,则返回True,或者result()返回结果并阻塞直到它准备好(如果它仍然没有(

最新更新