Both multiprocessing.map and joblib use only 1 cpu after upgrade from Ubuntu 10.10 to 12.04

对着背影说爱祢 提交于 2019-12-22 07:07:55

问题


I had some perfectly working python code which used multiprocessing module and loaded all 8 CPUs on my machine at 100%.

After I upgraded from Ubuntu 10.10 to 12.04 (the most evident thing, maybe I did something else that broke everything), it stopped working. After lots of debugging, I found that even in the simplest use case, both modules are only using 1 CPU:

from pylab import *
import multiprocessing as mp
from joblib import Parallel, delayed

def f(i):
    # Slow calculation
    x = 1
    for j in range(100000): x = cos(x)
    print i, x

if __name__ == '__main__':
    # Try to multiprocess with multiprocessing
    mp.Pool(processes=8).map(f, range(100))
    # Try to multiprocess with joblib
    Parallel(n_jobs=8)(delayed(f)(i) for i in range(100))

I need to use all 8 CPUs in my system. Any ideas of what I should look at to fix the issue?

EDIT:

As ali_m pointed out in a comment here and in the answer to Why does multiprocessing use only a single core after I import numpy? the problem is related to numpy messing up with cpu affinity. Calling

os.system('taskset -p 0xffffffff %d' % os.getpid())

Before I do any multiprocessing solved the problem for me.

来源:https://stackoverflow.com/questions/15168014/both-multiprocessing-map-and-joblib-use-only-1-cpu-after-upgrade-from-ubuntu-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!