繁体   English   中英

使用 Python 'while' 循环设置运行时间限制

[英]Setting a limit for running time with a Python 'while' loop

我有一些关于在 Python 中设置最大运行时间的问题。 事实上,我想使用 pdfminer 将 PDF 文件转换为 .txt。 问题是很多时候,有些文件无法解码并且需要很长时间。 所以我想设置time.time()将每个文件的转换时间限制为 20 秒。 另外,我在windows下运行,所以不能使用信号功能。

我成功地使用pdfminer.convert_pdf_to_txt()运行转换代码(在我的代码中它是“c”),但我无法在 while 循环中集成time.time() 在我看来,在以下代码中,while 循环和time.time()不起作用。

总之,我想:

  1. 将 PDF 文件转换为 .txt 文件

  2. 每次转换的时间限制为 20 秒。 如果超时,则抛出异常并保存一个空文件

  3. 将所有txt文件保存在同一个文件夹下

  4. 如果有任何异常/错误,仍然保存文件,但内容为空。

这是当前的代码:

import converter as c
import os
import timeit
import time

yourpath = 'D:/hh/'

for root, dirs, files in os.walk(yourpath, topdown=False):

    for name in files:

        t_end = time.time() + 20

        try:
            while time.time() < t_end:

                c.convert_pdf_to_txt(os.path.join(root, name))

                t = os.path.split(os.path.dirname(os.path.join(root, name)))[1]
                a = str(os.path.split(os.path.dirname(os.path.join(root, name)))[0])

                g = str(a.split("\\")[1])
                with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
                    newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))
                    print "yes"

            if time.time() > t_end:

                print "no"

                with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
                    newfile.write("")

        except KeyboardInterrupt:
           raise

        except:
            for name in files:
                t = os.path.split(os.path.dirname(os.path.join(root, name)))[1]
                a = str(os.path.split(os.path.dirname(os.path.join(root, name)))[0])

                g = str(a.split("\\")[1])
                with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
                    newfile.write("")

你有错误的方法。

如果当前时间戳低于结束时间戳(将始终为Truewhile您定义结束时间并立即进入while循环 所以进入了while循环,你就会陷入转换函数的困境。

我建议使用 Python 中已经包含的signal模块。 它允许您在n秒后退出函数。 这个 Stack Overflow answer 中可以看到一个基本的例子。

你的代码是这样的:

return astring
import converter as c
import os
import timeit
import time
import threading
import thread

yourpath = 'D:/hh/'

for root, dirs, files in os.walk(yourpath, topdown=False):
    for name in files:
        try:
            timer = threading.Timer(5.0, thread.interrupt_main)
            try:
                c.convert_pdf_to_txt(os.path.join(root, name))
            except KeyboardInterrupt:
                 print("no")

                 with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
                     newfile.write("")
            else:
                timer.cancel()
                t = os.path.split(os.path.dirname(os.path.join(root, name)))[1]
                a = str(os.path.split(os.path.dirname(os.path.join(root, name)))[0])
                g = str(a.split("\\")[1])

                print("yes")

                with open("D:/f/" + g + "&" + t + "&" + name + ".txt", mode="w") as newfile:
                    newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))

        except KeyboardInterrupt:
           raise

        except:
            for name in files:
                t = os.path.split(os.path.dirname(os.path.join(root, name)))[1]
                a = str(os.path.split(os.path.dirname(os.path.join(root, name)))[0])

                g = str(a.split("\\")[1])
                with open("D:/f/"+g+"&"+t+"&"+name+".txt", mode="w") as newfile:
                    newfile.write("")

只为未来:四个空格缩进,不要太多空格;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM