1

Python での最大実行時間の設定に関していくつか質問があります。実際、pdfminer を使用して PDF ファイルを .txt に変換したいと考えています。問題は、非常に多くの場合、一部のファイルがデコードできず、非常に長い時間がかかることです。time.time()そこで、各ファイルの変換時間を 20 秒に制限するように設定したいと思います。また、Windows で実行しているため、シグナル機能を使用できません。

(私のコードでは "c" です) で変換コードを実行することに成功しましたが、while ループにpdfminer.convert_pdf_to_txt()統合できませんでした。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("")
4

1 に答える 1

1

あなたは間違ったアプローチをしています。

終了時間を定義し、現在のタイムスタンプが終了タイムスタンプよりも低い場合はすぐにループに入りwhileます(常に になりますTrue)。そのため、whileループに入り、変換機能で行き詰まります。

signal既に Python に含まれているモジュールをお勧めします。n数秒後に関数を終了できます。基本的な例は、このスタック オーバーフローの回答で確認できます。

コードは次のようになります。

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

将来のために: 4つのスペースのインデントと空白が多すぎない;)

于 2016-11-22T14:47:33.103 に答える