设置while循环运行时间的python限制 [英] python setting limit for running time with while loop

查看:1566
本文介绍了设置while循环运行时间的python限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些与在python中设置最大运行时间有关的问题.实际上,我想使用pdfminer将pdf文件转换为.txt.问题是很多情况下,有些文件无法解码,并且需要很长时间.因此,我想设置time.time()以将每个文件的转换时间限制为20秒.此外,我在Windows下运行,因此无法使用信号功能.

I have some questions related to setting the maximum running time in python. In fact, I would like to use pdfminer to convert the pdf files to .txt. The problem is that very often, some files are not possible to decode and take extremely long time. So I want to set time.time() to limit the conversion time for each file to 20 seconds. In addition, I run under windows so I cannot use signal function.

我成功地使用pdfminer.convert_pdf_to_txt()运行了转换代码(在我的代码中是"c"),但是我无法将time.time()集成到while循环中.在我看来,在下面的代码中,while循环和time.time()不起作用.

I succeeded in running the conversion code with pdfminer.convert_pdf_to_txt() (in my code it is "c"), but I could not integrate the time.time() in the while loop. It seems to me that in the following code, the while loop and time.time() do not work.

总而言之,我想:

  1. 将pdf转换为txt

  1. convert the pdf to txt

每次转换的时间限制为20秒,如果时间用完,请抛出异常并保存一个空文件

time limit for each conversion is 20 sec, if it runs out of time, throw an excepetion and save an empty file

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

save all the txt files under the same folder

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

if there are any exceptions/errors, still save the file but with empty content.

这是当前代码:

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("")

推荐答案

您的方法有误.

您要做的是定义结束时间,并且如果当前时间戳小于结束时间戳,则立即进入while循环(将始终为True). 因此,进入了while循环,您陷入了转换功能的困境.

What you do is defining the end time and immediately entering the while loop if the current timestamp is lower than the end timestamp (will be always True). So the while loop is entered and you get stuck at the converting function.

我建议使用signal模块,该模块已包含在Python中.它允许您在n秒后退出功能.可以在此StackOverflow分析工具中看到一个基本示例.

I would suggest the signal module, which is already included in Python. It allows you to quit a function after n seconds. A basic example can be seen in this StackOverflow anser.

您的代码如下:

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("")

我真的希望这会有所帮助.如果您在理解代码更改时遇到问题,请随时询问.

I really hope this helps. If you have issues in understanding the code changes, feel free to ask.

为将来而生:缩进4个空格,不要有太多空格;)

这篇关于设置while循环运行时间的python限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆