2020.11.26
在使用multiprocessing.Pool来创建多个进程时,使用apply_async异步调用以提升运算效率,并行运算。但是这个函数的调用需要注意,最好参数不要省略func=和args=,args后面要有一个',',最重要的是,假如子进程的函数有返回值,需要将apply_async返回的值赋给一个变量,否则会产生阻塞。
model_ps = Pool(processes=6)
# ...
base_model = model_ps.apply_async(func=model.get_model, args=(method, model.train_eps, model.train_news,
model.save, model.save_path,
model.load, model.load_path, ))
# get_model有返回值, args最后有一个逗号。
2021.1.4
linux多进程都在一个cpu上跑,导致效率太低,使用taskset调整进程的cpu亲和性,python代码中加上一句
os.system("taskset -p 0xff %d" % os.getpid())
就可以在多个cpu上跑多进程了。