Python 多CPU編程

來源:互聯網
上載者:User

      因為工作需要,從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

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.