Python 多CPU编程

       因为工作需要,从Python 2.5开始学习Python,当然只是简单的使用Python完成一些工作,没有很少深入学习。一次,在一个多CPU的环境中,想用Python的多线程编程来达到使用完系统的CPU的目的,结果是不行的。最后发现,Python 2.6以后提供了一个multiprocessing的库,可以达到这个目的。

分享如下:

##多线程编程

[root@AS-1 bin]# cat z.py
# -*- coding:utf-8 -*-

import os
import threading
   
def test():
    while True:
        65535+65535
   
if __name__ == “__main__”:
    for n in range(20):
 print ‘Proc(%d) Start…’%n
        p = threading.Thread(target=test, args=())
        p.start()

##运行情况。见红色字体,只占用了一个CPU,系统CPU Idle 99.5%

[root@AS-1 bin]# ./python z.py &
Proc(0) Start…
Proc(1) Start…
Proc(2) Start…
Proc(3) Start…
Proc(4) Start…
Proc(5) Start…
Proc(6) Start…
Proc(7) Start…
Proc(8) Start…
Proc(9) Start…
Proc(10) Start…
Proc(11) Start…
Proc(12) Start…
Proc(13) Start…
Proc(14) Start…
Proc(15) Start…
Proc(16) Start…
Proc(17) Start…
Proc(18) Start…
Proc(19) Start…

[root@AS-1 bin]# top -n 1
top – 03:06:56 up 30 days, 31 min,  3 users,  load average: 1.25, 1.05, 0.65
Tasks: 320 total,   1 running, 300 sleeping,  19 stopped,   0 zombie
Cpu(s):  0.3%us,  0.1%sy,  0.0%ni, 99.5%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:  32953084k total,  7391604k used, 25561480k free,   276480k buffers
Swap: 34996216k total,   347648k used, 34648568k free,  3761504k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                         
25736 root      16   0  273m 4380 1664 S 127.6  0.0   0:34.65 python                                                                                                                          
25783 root      15   0 12864 1164  728 R  2.0  0.0   0:00.01 top                                                                                                                              
    1 root      15   0 10344  564  528 S  0.0  0.0   0:02.16 init

## 多CPU编程, 见红色字体,用到了多个CPU(核/线程),系统CPU Idle为0.0%

 [root@AS-1 bin]# cat x.py
# -*- coding:utf-8 -*-

import os
from multiprocessing import *

def test():
    while True:
        65535+65535

if __name__ == “__main__”:
    for n in range(20):
        print “Proc(%d) Start…”%n
        p = Process(target = test, args = [])
        p.start()

[root@AS-1 bin]# ./python x.py
Proc(0) Start…
Proc(1) Start…
Proc(2) Start…
Proc(3) Start…
Proc(4) Start…
Proc(5) Start…
Proc(6) Start…
Proc(7) Start…
Proc(8) Start…
Proc(9) Start…
Proc(10) Start…
Proc(11) Start…
Proc(12) Start…
Proc(13) Start…
Proc(14) Start…
Proc(15) Start…
Proc(16) Start…
Proc(17) Start…
Proc(18) Start…
Proc(19) Start…

[root@AS-1 bin]# top
top – 03:09:44 up 30 days, 33 min,  3 users,  load average: 6.20, 2.14, 1.07
Tasks: 341 total,  22 running, 299 sleeping,  20 stopped,   0 zombie
Cpu(s):100.0%us,  0.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  32953084k total,  7407396k used, 25545688k free,   276568k buffers
Swap: 34996216k total,   347648k used, 34648568k free,  3762068k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
25940 root      25   0 83464 3364  468 R 100.0  0.0   0:21.25 python                                                                                                                          
25944 root      25   0 83464 3364  468 R 100.0  0.0   0:21.24 python                                                                                                                          
25945 root      25   0 83464 3364  468 R 100.0  0.0   0:21.18 python                                                                                                                          
25946 root      25   0 83464 3364  468 R 100.0  0.0   0:21.24 python                                                                                                                          
25948 root      25   0 83464 3368  468 R 100.0  0.0   0:21.15 python                                                                                                                          
25949 root      25   0 83464 3368  468 R 100.0  0.0   0:21.23 python                                                                                                                          
25951 root      25   0 83464 3372  468 R 100.0  0.0   0:21.21 python                                                                                                                          
25952 root      25   0 83464 3372  468 R 100.0  0.0   0:21.23 python                                                                                                                          
25953 root      25   0 83464 3380  468 R 100.0  0.0   0:21.22 python                                                                                                                          
25954 root      25   0 83464 3380  468 R 100.0  0.0   0:21.20 python                                                                                                                          
25943 root      25   0 83464 3364  468 R 99.6  0.0   0:21.21 python                                                                                                                           
25950 root      25   0 83464 3368  468 R 99.6  0.0   0:21.15 python                                                                                                                           
25959 root      25   0 83464 3388  468 R 50.5  0.0   0:10.53 python                                                                                                                           
25947 root      25   0 83464 3364  468 R 50.2  0.0   0:10.63 python                                                                                                                           
25957 root      25   0 83464 3384  468 R 50.2  0.0   0:10.53 python                                                                                                                           
25958 root      25   0 83464 3388  468 R 50.2  0.0   0:10.56 python                                                                                                                           
25941 root      25   0 83464 3364  468 R 49.8  0.0   0:10.72 python                                                                                                                           
25942 root      25   0 83464 3364  468 R 49.8  0.0   0:10.70 python                                                                                                                           
25955 root      25   0 83464 3384  468 R 49.8  0.0   0:10.62 python                                                                                                                           
25956 root      25   0 83464 3384  468 R 49.8  0.0   0:10.61 python                                                                                                                           
25976 root      15   0 12868 1276  816 R  0.3  0.0   0:00.03 top                                                                                                                              
    1 root      15   0 10344  564  528 S  0.0  0.0   0:02.16 init

发表评论

电子邮件地址不会被公开。 必填项已用*标注

机器人检查 *