python - Multiprocessing objects with namedtuple - Pickling Error -


i having trouble using namedtuples in objects want put multiprocessing. receiving pickling error. tried couple of things other stackoverflow posts, not succeed. here structure of code:

package_main, test_module

 import myprogram.package_of_classes.data_object_module  import ....obj_calculate   class test(object):        if __name__ == '__main__':              my_obj=create_obj('myobject',['f1','f2'])              input = multiprocessing.queue()              output = multiprocessing.queue()              input.put(my_obj)              j=process(target=obj_calculate, args=(input,output))              j.start() 

package_of_classes, data_object_module

 import collections  import ....load_flat_file   def get_ntuple_format(obj):      nt_fields=''      fld in obj.fields:          nt_fields=nt_fields+fld+', '      nt_fields=nt_fields[0:-2]      ntuple=collections.namedtuple('ntuple_format',nt_fields)      return ntuple   class data_obj:     def __init__(self, name,fields):         self.name=name         self.fields=fields         self.ntuple_form=get_ntuple_format(self)        def calculate(self):         self.file_read('c:/files','division.txt')      def file_read(self,data_directory,filename):         output=load_flat_file(data_directory,filename,self.ntuple_form)         self.data=output 

utils_package,utils_module

def create_dataobj(name,fields):     locals()[name]=data_obj(name,fields)     return locals()[name]    def obj_calculate(input,output):        obj=input.get()     obj.calculate()     output.put(obj) 

loads_module

def load_flat_file(data_directory,filename,ntuple_form):      csv.register_dialect('csvrd', delimiter='\t', quoting=csv.quote_none)      listoftuples=[]      open(os.path.join(data_directory,filename), 'rb') f:           reader = csv.reader(f,'csvrd')           line in reader:                if line:                    listoftuples.append(ntuple_form._make(line))      return listoftuples 

and error getting is:

picklingerror: picklingerror: can't pickle  class '__main__ . ntuple_format: it's not same object __ main __. ntuple_format 

p.s. extracted sample code large project, please ignore minor inconsistencies.

you cannot pickle class (in case, named tuple) create dynamically (via get_ntuple_format). class picklable, it has defined @ top level of importable module.

if have few kinds of tuples need support, consider defining them in advance, @ top level of module, , picking right 1 dynamically. if need dynamic container format, consider using dict instead.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -