0

I have my code that is sprawning multiple processes to check the count of files and maintaining the records in the database. The code which is working is mentioned below :

import multiprocessing  as mp
from multiprocessing import Pool
import os
import time
import mysql.connector

"""Function to check the count of the file"""
def file_wc(fname):
        with open('/home/vaibhav/Desktop/Input_python/'+ fname) as f:
            count = sum(1 for line in f)            
        return (fname,count)    

class file_audit:

    def __init__(self):      
        """Initialising the constructor for getting the names of files 
        and refrencing the outside class function"""

        folder = '/home/vaibhav/Desktop/Input_python'
        self.fnames = (name for name in os.listdir(folder))
        self.file_wc=file_wc   

    def count_check(self):
        "Creating 4 worker threads to check the count of the file parallelly"

        pool = Pool(4)
        self.m=list(pool.map(self.file_wc, list(self.fnames),4))
        pool.close()
        pool.join()

    def database_updation(self):
        """To maintain an entry in the database with details 
        like filename and recrods present in the file"""

        self.db = mysql.connector.connect(host="localhost",user="root",password="root",database="python_showtime" )
# prepare a cursor object using cursor() method
        self.cursor = self.db.cursor()
        query_string = ("INSERT INTO python_showtime.audit_capture"
                "(name,records)"
                "VALUES(%s,%s)")
        #data_user = (name,records)
        for each in self.m:    

            self.cursor.execute(query_string, each)
            self.db.commit()
        self.cursor.close()
        start_time = time.time()
        print("My program took", time.time() - start_time, "to run")

#if __name__ == '__main__':
x=file_audit()
x.count_check()  #To check the count by sprawning multiple processes
x.database_updation() #To maintain the entry in the database

Point to be considered

Now if i put my function inside the class and comment self.file_wc=file_wc in the constructor section i get the Error can't pickle on generator objects. I got some fair understanding like we cannot pickle some objects,So want to know what exactly is happening at the background in very simple terms. I got the reference from here or here to make the code working

user7422128
  • 902
  • 4
  • 17
  • 41
  • FYI, try using `multiprocess` instead of `multiprocessing` – Eli Korvigo Sep 11 '17 at 18:49
  • how will it help me considering efficiency of the program here?? – user7422128 Sep 11 '17 at 18:50
  • My comment is not a direct answer to your question (hence it is a comment). I'm just showing you a module, that can work with far more object types than `multiprocessing` (because it uses `dill` instead of `pickle`) – Eli Korvigo Sep 11 '17 at 19:02
  • Didn't heard about multiprocess module anytime.A link shared would be better. – user7422128 Sep 11 '17 at 19:07
  • [PyPI](https://pypi.python.org/pypi/multiprocess) and [github](https://github.com/uqfoundation/multiprocess). It mirrors `multiprocessing` in terms of API, hence you can simply change the import. – Eli Korvigo Sep 11 '17 at 19:11
  • No offense to you @EliKorvigo but not in a mood to alter my code at this time else i need to change a lot of other stuffs as well – user7422128 Sep 11 '17 at 19:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154171/discussion-between-eli-korvigo-and-codaholic). – Eli Korvigo Sep 11 '17 at 19:52

0 Answers0