The python binding process executes affinity on a CPU

Source: Internet
Author: User

The utilization of the multiprocessing process on a multi-CPU machine. And then wrote a script in i5 (4 cores), up 4 processes, found that only one CPU ran relatively high, and then feel not very comfortable.

Specify a CPU to execute a process, Python has a third-party library http://pypi.python.org/pypi/affinity/0.1.0, basically is called the system's affinity mask API.

It seems to be a bit of a use to have a try on windows.

Look at the code, a total of 3 methods, it is very convenient to use.
_get_handle_for_pid (PID, Ro=true)
Get the process through a PID
Get_process_affinity_mask (PID)
Get the current affinity mask correlation mask for this process by PID, return a long shape (' 2l ', that is, using 2 of this CPU)
Set_process_affinity_mask (PID, value)
Bind PID This process to the CPU referred to by value (Set_process_affinity_mask (3516, 1), is to use 1 of this CPU to perform PID 3516 this process)

>>>import multiprocessing, time, signal>>>Import Affinity #我用multiprocessing创建了个进程>>>p = multiprocessing. Process (Target=time.sleep, args= ( +,)) >>>P.start ()>>>pid=P.pid>>>Print PID5604#先看了下5604这个进程用的是那个cpu>>>affinity.get_process_affinity_mask (PID)3L#将5604这个进程用2这个cpu跑>>>affinity.set_process_affinity_mask (PID,2L)  3L>>>affinity.get_process_affinity_mask (PID)2L  

The python binding process executes affinity on a CPU

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.