title: python threading.timer set time limit when program runs out of time
body: <p>I have some questions related to setting the maximum running time of a function in Python. In fact, I would like to use <code>pdfminer</code> to convert the <code>.pdf</code> files to <code>.txt</code>. </p>
  
  <p>The problem is that very often, some files are not possible to decode and take extremely long time. So I want to set <code>threading.Timer()</code> to limit the conversion time for each file to 5 seconds. In addition, I run under windows so I cannot use the <code>signal</code> module for this.</p>
  
  <p>I succeeded in running the conversion code with <code>pdfminer.convert_pdf_to_txt()</code> (in my code it is "<code>c</code>"), but I am not sure that the in the following code, <code>threading.Timer()</code> works. (I don't think it properly constrains the time for each processing)</p>
  
  <p>In summary, I want to:</p>
  
  <ol>
  <li><p>Convert the pdf to txt</p></li>
  <li><p>Time limit for each conversion is 5 sec, if it runs out of time, throw an exception and save an empty file</p></li>
  <li><p>Save all the txt files under the same folder</p></li>
  <li><p>If there are any exceptions/errors, still save the file but with empty content.</p></li>
  </ol>
  
  <p>Here is the current code: </p>
  
  <pre><code>import converter as c
  import os
  import timeit
  import time
  import threading
  import thread
  
  yourpath = 'D:/hh/'
  
  def iftimesout():
      print("no")
  
      with open("D:/f/"+g+"&amp;"+t+"&amp;"+name+".txt", mode="w") as newfile:
          newfile.write("")
  
  
  for root, dirs, files in os.walk(yourpath, topdown=False):
      for name in files:
          try:
             timer = threading.Timer(5.0,iftimesout)
             timer.start()
             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+"&amp;"+t+"&amp;"+name+".txt", mode="w") as newfile:
                  newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))
                  print("yes")
  
             timer.cancel()
  
           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+"&amp;"+t+"&amp;"+name+".txt", mode="w") as newfile:
                       newfile.write("") 
  </code></pre>