Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14126

(Multi processing) Why the code written under "if __name__ == '__main__':" Without indentation runs several times, equal to num of cores, in PyCharm

$
0
0

Followings is my code:

import multiprocessingimport timedef list_function_multiprocessing(setlist, function):    from multiprocessing import Pool    num_cores = multiprocessing.cpu_count()    pool = Pool(processes=num_cores-4)    start = time.time()    results_list = pool.starmap(function, setlist)    pool.close()    pool.join()    return results_list, time.time() - startodKeyList = list((tGr, i[0], i[1], 5) for i in scaled_od_dic)if __name__ == '__main__':    res01, res01CTime = list_function_multiprocessing(odKeyList, kth_shortest_path)    print(res01CTime)print('het')

As you can see I have print('het') outside of the if __name__ == '__main__':. the result in python is: Result

as you can see in results, the line of code print('het') has been executed 16 time which is equal to the number of cores engaged in process (20-4). I want to write a big chunk of code this part, and my concern is the other chunk is executed several times as well.

I appreciate if anyone can help me

I have tried writing the function out of the main script in a separate file. but it didn't worked without if __name__ == '__main__': idiom in the main script. The only way that i could make it work was the time that i put print('het') under if __name__ == '__main__': idiom.


Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>