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