![](/img/trans.png)
[英]How to make sure that only one instance of a thread is running in Python?
[英]Python: Make sure concurrent Thread is finished
我正在尝试编写一个适用于线程的 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()
返回结果并阻塞直到它准备好(如果它仍然没有)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.