python 导入numpy 导致多进程绑定同一个CPU问题解决方法
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 affinityIn [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 npIn [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 npimport affinityimport multiprocessingaffinity.set_process_affinity_mask(0,2**multiprocessing.cpu_count()-1) |
python 导入numpy 导致多进程绑定同一个CPU问题解决方法的更多相关文章
- Python:导入numpy报错 No module named numpy
Numpy是python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比python自身的嵌套列表结构要高效的多.但是在使用numpy时可能会出错(如上图). 解决办法:下载安装对应版本 ...
- 全量导入数据 导致solr内存溢出 崩溃问题解决
在 data-config.xml 文件中 增加一个参数即可: batchSize="-1"
- VS2015 C# 编写USB通信上位机时,改变net框架导致DLL调用失败的问题解决方法
最近在写USB通信的上位机,调用了windows里的DLL,开发环境:64位WIN7 .VS2015.NET4.5.2:开发完成后在自己的电脑可用,在32位电脑.NET其他版本以及WIN10的环境下不 ...
- Python 安装beautifulsoup4遇到No module named setuptools问题解决方法
背景说明: 电脑win7-32 在Python 3.3.5下安装beautifulsoup4 4.6.0(下载链接https://pypi.org/project/beautifulsoup4/#fi ...
- eclipse导入myeclipse的web项目没法识别问题解决方法
1.进入项目目录,找到.project文件,打开. 2.找到<natures>...</natures>代码段. 3.在第2步的代码段中加入如下标签内容并保存: <nat ...
- 关于ajax请求后js绑定事件失效问题解决方法
<script> $(function(){ $(document).on('click', '.add' ,function(){ window.location.href=" ...
- 源码编译路径错误导致的Apache 无法重启问题解决方法
问题现象: 第一次源码编译安装Apache设置路径错误,安装到/usr/local/src/ 目录下了. 删掉该目录下的安装文件,重新编译安装到/usr/local/目录下 重启apache服务时报这 ...
- 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)
最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: ...
- python并发编程之多进程、多线程、异步、协程、通信队列Queue和池Pool的实现和应用
什么是多任务? 简单地说,就是操作系统可以同时运行多个任务.实现多任务有多种方式,线程.进程.协程. 并行和并发的区别? 并发:指的是任务数多余cpu核数,通过操作系统的各种任务调度算法,实现用多个任 ...
随机推荐
- Modbus
Modbus 串行链路协议是一个主-从协议.在同一时刻,只有一个主节点连接于总线,一个或多个子节点 (最大编号为 247 ) 连接于同一个串行总线. Modbus 通信总是由主节点发起.子节点在没有收 ...
- windows下配置host不生效问题
今天遇到了host配置之后不生效的问题,原因是文本编辑器用的是非windows格式,再notepad++右下角切换一下即可,将notepad++ 视图-显示符号-显示所有字符勾上之后可以看到windo ...
- 剑指Offer_编程题_5
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. class Solution { public: void push(int node) { if( ...
- Hadoop问题:java.net.SocketException: Network is unreachable
问题描述:Failed on local exception: java.net.SocketException: Network is unreachable; Host Details : loc ...
- angular,vue,react的父子通信
父子通信 父传子 vue: 父组件:<child :msg="datamsg" ></child> //子组件的msg属性上加数据,datamsg是数据 子 ...
- 在Ajax返回多个值
<html> <head> <title>AjaxTest</title> <script type="text/javascript& ...
- Linux学习笔记:【004】Linux内核代码风格
Chinese translated version of Documentation/CodingStyle If you have any comment or update to the c ...
- HDU 1025(最长上升子序列)
题意是要在两条平行线间连点,要在线不交叉的前提下尽可能多的连线,问最多能连多少条线. 现假定题中所给的是 9 组点,分别是:1—3,2—8,3—5,4—9,5—2,6—4,7—6,8—7,9—1,如图 ...
- 前端常用UI框架
移动端UI框架 Mint UI(饿了么团队) 中文官网:http://mint-ui.github.io/#!/en 饿了么前端团队推出的基于Vue.js的移动端组件库 GitHub地址:https: ...
- 如何实现从 Redis 中订阅消息转发到 WebSocket 客户端
PHP 的redis扩展是阻塞式 IO ,使用订阅/发布模式时,会导致整个进程进入阻塞.因此必须使用Swoole\Redis异步客户端来实现. 实例代码 $server = new swoole_we ...