Python并行计算
多进程 在 python 中使用 multiprocessing 时, multiprocessing.Pool 必须写在 if __name__ == '__main__': 下,否则会报错。不是很实用。 import multiprocessing def fun ( p ) : print ( p ) return p [ 0 ] , p [ 1 ] def do_something ( x ) : v = pow ( x , 2 ) return v if __name__ == '__main__' : t_dir = "C:/Users/user/Desktop/u" dir_list = os . listdir ( t_dir ) lst = [ ] for i in range ( len ( dir_list ) ) : lst . append ( ( dir_list [ i ] , i ) ) p = multiprocessing . Pool ( 4 ) # items = [x for x in range(1, 100000000)] dis = p . map ( fun , lst ) p . close ( ) p . join ( ) 来源: https://blog.csdn.net/qq_40250862/article/details