Category: python

Python threading keyboard interrupt

Proper async thread termination:

pool = ThreadPool(threads_number)

try:
  results = []
  for root, dirs, files in os.walk('.'):
  for f in files:
    results.append( pool.apply_async(process_file, (f, root, file_counter, numfiles)) )
    file_counter += 1
    pool.close()
  except KeyboardInterrupt:
    pool.terminate()
  finally:
    pool.join()