Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import multiprocessing as mp
  2. from multiprocessing import Pool
  3. import os
  4. import time
  5. import mysql.connector
  6.  
  7. """Function to check the count of the file"""
  8. def file_wc(fname):
  9. with open('/home/vaibhav/Desktop/Input_python/'+ fname) as f:
  10. count = sum(1 for line in f)
  11. return (fname,count)
  12.  
  13. class file_audit:
  14.  
  15. def __init__(self):
  16. """Initialising the constructor for getting the names of files
  17. and refrencing the outside class function"""
  18.  
  19. folder = '/home/vaibhav/Desktop/Input_python'
  20. self.fnames = (name for name in os.listdir(folder))
  21. self.file_wc=file_wc
  22.  
  23. def count_check(self):
  24. "Creating 4 worker threads to check the count of the file parallelly"
  25.  
  26. pool = Pool(4)
  27. self.m=list(pool.map(self.file_wc, list(self.fnames),4))
  28. pool.close()
  29. pool.join()
  30.  
  31. def database_updation(self):
  32. """To maintain an entry in the database with details
  33. like filename and recrods present in the file"""
  34.  
  35. self.db = mysql.connector.connect(host="localhost",user="root",password="root",database="python_showtime" )
  36. # prepare a cursor object using cursor() method
  37. self.cursor = self.db.cursor()
  38. query_string = ("INSERT INTO python_showtime.audit_capture"
  39. "(name,records)"
  40. "VALUES(%s,%s)")
  41. #data_user = (name,records)
  42. for each in self.m:
  43.  
  44. self.cursor.execute(query_string, each)
  45. self.db.commit()
  46. self.cursor.close()
  47. start_time = time.time()
  48. print("My program took", time.time() - start_time, "to run")
  49.  
  50. #if __name__ == '__main__':
  51. x=file_audit()
  52. x.count_check() #To check the count by sprawning multiple processes
  53. x.database_updation() #To maintain the entry in the database
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement