Set CPU affinity in python

Source: Internet
Author: User

For unknown reasons, a python program always occupies one CPU core, resulting in one full-utilization, the other low, and unbalanced. Although process relevance can be set to all processors in task management, it is too troublesome to set each time. You can check the windows API settings, but you must set them in python, you need to do some work.

First, python must support pywin32, address: http://sourceforge.net/projects/pywin32/

Secondly, download affinity module installation, here provides access to SetProcessAffinityMask method, address: http://pypi.python.org/pypi/affinity

Then you can use it. For example, add the code at the beginning of the program

1 import affinity # need pywin322 import os3 4 affinity.set_process_affinity_mask(os.getpid(), 3L) # set all cpu to run

Run the program. In the task manager, you can see that the program uses two CPUs.

Note: The process is bound to the specified cpu: SetProcessAffinityMask (GetCurrentProcess (), dwMask );

DwMask indicates the or operation value of the CPU sequence number. 1 (0001) indicates that only CPU 1 is run, 2 (0010) indicates that only CPU 2 is run, and 3 (0011) indicates that CPU 1 and CPU2 can be run, and so on.

I haven't found a function for python to get the number of CPU cores, so I checked it manually. It seems that win32con can do this, and I haven't studied it in linux...

 

Appendix:

CPU affinity

According to the default settings, when the system assigns a thread to the processor, Windows uses soft affinity for operations. This means that if all other factors are the same, it will try to run the thread on the processor it was last running. Keeping threads on a single processor helps reuse data that is still in the memory cache of the processor.

There is a new computer structure called NUMA (non-uniform memory access). In this structure, the computer contains several plug-in boards, each plug-in board has four CPUs and its own memory zone.
When the memory accessed by the CPU is the memory on its own plug-in board, the NUMA system has the best performance. If the CPU needs to access the internal memory located on another plug-in board, it will cause huge performance degradation. In such an environment, you need to restrict the threads from a process to run on the CPU that shares the same plug-in version. To meet the needs of this computer structure, Windows allows you to set the affinity of processes and threads. In other words, you can control which CPU can run certain threads. This is called hard affinity. Note that sub-processes can inherit the affinity of processes.

Note:

(1) No matter how many CPUs the computer actually has, Windows98 and the previous system only use one CPU, the above API is not supported.


(2) in most environments, changing the affinity of threads will affect the scheduling program's ability to effectively port threads between various CPUs, which can use the CPU time most effectively.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.