python 如果有导入numpy模块的import语句,会导致默认将多进程程序的每个进程都绑定到同一个CPU core上,

失去了多进程在多核CPU上的性能优越性,这和CPU affinity(CPU亲和性)有关,解决办法:

导入affinity包,执行:

affinity.set_process_affinity_mask(0,2**multiprocessing.cpu_count()-1)

以下是英文文档原文,供参考:

Python refuses to use multiple cores – solution

 

I was trying to get parallel Python to work and I noticed that if I run two Python scripts simultaneously – say, in two different terminals – they use the same core. Hence, I get no speedup from multiprocessing/parallel Python. After some searching around, I found out that in some circumstances importing numpy causes Python to stick all computations in one core. This is an issue with CPU affinity, and apparently it only happens for some mixtures of Numpy and BLAS libraries – other packages may cause the CPU affinity issue as well.

There’s a package called affinity (Linux only AFAIK) that lets you set and get CPU affinity. Download it, run python setup.py install, and run this in Python or ipython:

1
2
3
4
In [1]: import affinity
 
In [2]: affinity.get_process_affinity_mask(0)
Out[2]: 63

This is good: 63 is a bitmask corresponding to 111111 – meaning all 6 cores are available to Python. Now running this, I get:

1
2
3
4
In [4]: import numpy as np
 
In [5]: affinity.get_process_affinity_mask(0)
Out[5]: 1

So now only one core is available to Python. The solution is simply to set the CPU affinity appropriately after import numpy, for instance:

1
2
3
4
5
import numpy as np
import affinity
import multiprocessing
 
affinity.set_process_affinity_mask(0,2**multiprocessing.cpu_count()-1)

python 导入numpy 导致多进程绑定同一个CPU问题解决方法的更多相关文章

  1. Python:导入numpy报错 No module named numpy

    Numpy是python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比python自身的嵌套列表结构要高效的多.但是在使用numpy时可能会出错(如上图). 解决办法:下载安装对应版本 ...

  2. 全量导入数据 导致solr内存溢出 崩溃问题解决

    在 data-config.xml 文件中 增加一个参数即可: batchSize="-1"    

  3. VS2015 C# 编写USB通信上位机时,改变net框架导致DLL调用失败的问题解决方法

    最近在写USB通信的上位机,调用了windows里的DLL,开发环境:64位WIN7 .VS2015.NET4.5.2:开发完成后在自己的电脑可用,在32位电脑.NET其他版本以及WIN10的环境下不 ...

  4. Python 安装beautifulsoup4遇到No module named setuptools问题解决方法

    背景说明: 电脑win7-32 在Python 3.3.5下安装beautifulsoup4 4.6.0(下载链接https://pypi.org/project/beautifulsoup4/#fi ...

  5. eclipse导入myeclipse的web项目没法识别问题解决方法

    1.进入项目目录,找到.project文件,打开. 2.找到<natures>...</natures>代码段. 3.在第2步的代码段中加入如下标签内容并保存: <nat ...

  6. 关于ajax请求后js绑定事件失效问题解决方法

    <script> $(function(){ $(document).on('click', '.add' ,function(){ window.location.href=" ...

  7. 源码编译路径错误导致的Apache 无法重启问题解决方法

    问题现象: 第一次源码编译安装Apache设置路径错误,安装到/usr/local/src/ 目录下了. 删掉该目录下的安装文件,重新编译安装到/usr/local/目录下 重启apache服务时报这 ...

  8. 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

  9. python并发编程之多进程、多线程、异步、协程、通信队列Queue和池Pool的实现和应用

    什么是多任务? 简单地说,就是操作系统可以同时运行多个任务.实现多任务有多种方式,线程.进程.协程. 并行和并发的区别? 并发:指的是任务数多余cpu核数,通过操作系统的各种任务调度算法,实现用多个任 ...

随机推荐

  1. Vue(基础六)_嵌套路由(续)

    一.前言                  1.路由嵌套里面的公共路由                  2.keep-alive路由缓存                  3.导航守卫 二.主要内容 ...

  2. Hibernate的入门(增删改查):

    注意:本次的记录是在上一篇Hibernate入门的基础上应用的 1.目录 2.实体类修改 package com.itheima.domain; /* * 客户的javaBean * @author ...

  3. 用jQuery写的轮播图

    效果图: GitHub地址:https://github.com/123456abcdefg/Javascript 大家可以下载源码查看. 与前一篇写的轮播图实现的效果一致,这个是用jQuery写的, ...

  4. 面向对象【林老师版】:__init__定制自己独有的特征(三)

    本节内容 1.是如何产生对象 2.实例化的步骤 3.类即类型 一.是如何产生对象? __init__方法用来为对象定制对象自己独有的特征 1.stu1=LuffyStudent()调用报错 1.代码 ...

  5. [NIO-1]缓冲区

    常用的是ByteBuffer.CharBuffer

  6. ruby数组操作方法汇总

    1.数组定义 arr1 = [] arr2 = Array.new arr3 = ['1','2','3'] 2.输出 print arr3,"\n" #123 puts arr3 ...

  7. 2018牛客网暑期ACM多校训练营(第一场)A Monotonic Matrix(LGV)

    题意 分析 考虑01和12的分界线是(n, 0)到(0,m)的两条不相交(可重合)路径分界线以及分界线以上的点是一种,分界线下是一种平移其中一条变成(n-1, -1)到(-1,m-1); 此时起点为{ ...

  8. javaScript ES5常考面试题总结

    js的六种原始值 boolean null undefined number string symbol 坑1: 首先原始类型存储的都是值,是没有函数可以调用的,比如 undefined.toStri ...

  9. mysql驱动参数变化

    在java平台使用的mysql jdbc驱动为:mysql-connector-java. 在项目中添加如下依赖: <dependency> <groupId>mysql< ...

  10. 一次针对多台服务器交互式主机命令采集Python脚本编写

    [环境介绍]   系统环境:Linux + Python 2.7.10(监控主机)   [背景描述] 需求:每次节假日或者重要时间时,需要对数据库主机信息进行检查,比如主机空间使用率之类.有时候需要执 ...