Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. def write_to_csv(queue):
  2. file_path = os.path.join(os.getcwd(), 'test_dir', "writer.csv")
  3. ofile = open(file_path, "w")
  4. job_writer = csv.writer(ofile, delimiter='a')
  5. while 1:
  6. line = queue.get()
  7. if line == 'kill':
  8. print("Kill Signal received")
  9. break
  10. if line:job_writer.writerow([str(line).strip()])
  11. ofile.close()
  12.  
  13. def worker_main(file_queue, writer_queue):
  14. print os.getpid(),"working"
  15. while not file_queue.empty():
  16. file_name = file_queue.get(True)
  17. # somewhere in process_file writer_queue.put(line_resp) is called
  18. # for every line in file_name
  19. process_file(file_name, writer_queue)
  20.  
  21.  
  22. if __name__ == "__main__":
  23. file_queue = multiprocessing.Queue()
  24. output_queue = multiprocessing.Queue()
  25.  
  26. writer_pool = multiprocessing.Pool(1, write_to_csv, (output_queue,))
  27.  
  28. cwd = os.getcwd()
  29. test_dir = 'test_dir'
  30. file_list = os.listdir(os.path.join(cwd, test_dir))
  31. for file_name in file_list:
  32. file_queue.put(file_name)
  33.  
  34. reader_pool = multiprocessing.Pool(3, worker_main, (file_queue, output_queue))
  35. reader_pool.close()
  36. reader_pool.join()
  37.  
  38. output_queue.put("kill")
  39.  
  40. print("Finished execution")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement