Import Python multiprocessing main function in another Python file as a module -


i physicist , new python migrated matlab. need implement multiprocessing perform calculations genetic optimization algorithm.

i have managed make multiprocessing work in single python file using information found on-line. example code created replicate problem (it doesn't useful).

import numpy np import multiprocessing mp      def fun1((index, array)):      lst_1=[]     ar_i in array:         lst_1.append(ar_i+index)      return np.sum(np.squeeze(lst_1))          def main(n):     arr=np.arange(1,n)      lst=[]     elem in arr:                 lst.append((elem,arr))       p = mp.pool(8)     a=p.map(fun1, lst)          return           if __name__ == "__main__":     ans=main(30)  

if run python console gives me answer. python console can import function python file , works. example:

import example1 ans2=example1.main(20) 

but if in python file (for example main genetic algorithm file) , import function again , try run as:

import example1 ans3=example1.main(10) 

i error message:

"runtimeerror:              attempt start new process before current process             has finished bootstrapping phase.              means on windows , have             forgotten use proper idiom in main module:                  if __name__ == '__main__':                     freeze_support()                     ...              "freeze_support()" line can omitted if program             not going frozen produce windows executable." 

how can solve problem? searched online haven't been able figure out. perhaps missing obvious. thank you.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -