1

I am building a program that verifies media and doc files within a given file tree. When it tries to read a pdf (with PyPDF2), it will occasionally freeze the program. I want to implement a timeout on the function so that it just times out and logs an error rather than crashing. Here is my code for the PDF function:

def pdf_verify(file, good_files, bad_files):
    try:
        PyPDF2.PdfFileReader(open(file, "rb"))
        good_files.append(file)
    except:
        bad_files.append(file)

    return good_files, bad_files

It tries to open the PDF file with PyPDF2, and if it can't be opened, it catches the exception and appends the file to a bad file list. I know there is a way to do this on Linux, but I need the program to be runnable on Windows. Is there any way to do this? Thanks!

chepner
  • 446,329
  • 63
  • 468
  • 610
Chris Ray
  • 31
  • 2

1 Answers1

1

There are multiple ways to handle this problem:

  1. Use threads and monitor the time spent on that function
  2. Using signals - Check limit execution time of a function
  3. Wait that the program does an exception and catch it with except
Wondercricket
  • 7,138
  • 2
  • 37
  • 53
Vasco Lopes
  • 141
  • 9