1

I want to define a function like the following in a class:

class multiprocessing_np_arr_base_class(object):
    def pool_init(self, _shr_var1, _shar_var2, _shr_var3,...):
        global shr_var1
        shr_var1 = _shr_var1
        global shr_var2
        shr_var2 = _shr_var2
        global shr_var3
        shr_var3 = _shr_var3
        ...

The pool_init is very tedious to write. Is it possible to define a function that automatically do the two lines (global x, x = _x) for each variable x given to the function?

The pool_init function is an imitation of J.F. Sebastian's answer at Use numpy array in shared memory for multiprocessing. I am currently using python 2.7.

Thanks for your help.

Community
  • 1
  • 1
rxu
  • 1,369
  • 1
  • 11
  • 29
  • 1
    Use a dictionary instead of this many variables. – Daniel May 29 '16 at 14:17
  • that may work. just use one dictionary. let me try if that works for sharing numpy array between processes – rxu May 29 '16 at 14:21
  • may be can do this. in the __ init __ function, scan locals() for all self.shr_xyz . In init, define self.list_shr = the list of string name that refers to those xyz. Then, in pool_init, global all in self.list_shr. Set each ith variable refereed by elements of self.list_shr equal to the ith argument received by pool_init. – rxu May 29 '16 at 14:26
  • That worked. Thanks Daniel. – rxu May 29 '16 at 18:55

1 Answers1

1

I would suggest you implement something like this:

shr_var = {}
class multiprocessing_np_arr_base_class(object):
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            shr_var[k] = v

Then the usage would be:

a  = multiprocessing_np_arr_base_class(a=2,b=3)

shr_var
Out[15]: {'a': 2, 'b': 3}
2342G456DI8
  • 1,819
  • 3
  • 16
  • 29