[英]Python: Wait for process to finish before proceeding in loop?
I'm attempting to use PDFkit to create PDFs of a site and I'm having some trouble: 我正在尝试使用PDFkit创建网站的PDF,但遇到了一些麻烦:
for u in urls:
for c in u.children:
pdfkit.from_url(c, "test.pdf", configuration=config)
Running pdfkit
on a single URL works fine, but trying to use it in a for
loop causes problems. 在单个URL上运行pdfkit
可以正常工作,但是尝试在for
循环中使用它会导致问题。 I think it's because it takes a while for each PDF to be generated. 我认为这是因为生成每个PDF需要一段时间。
Is there a way I could wait while pdfkit
is running, and only proceed with the next URL when it has finished with the previous? 有什么方法可以让我等待pdfkit
运行,而只有在下一个URL与上一个URL结束时才继续处理?
You need async/wait function for python. 您需要python的异步/等待功能。
so, look at these 所以,看看这些
http://stackabuse.com/python-async-await-tutorial/ http://stackabuse.com/python-async-await-tutorial/
https://docs.python.org/3/library/asyncio-task.html https://docs.python.org/3/library/asyncio-task.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.