Because of work requirements, I learned python from Python 2.5. Of course, I simply used python to do some work, but I didn't have to study it in depth.
Once, in a multi-CPU environment, it is impossible to use Python multi-thread programming to achieve the purpose of using the CPU of the system. Last found,
Python 2.6 and later provide a multiprocessing library for this purpose.
Share as follows:
# Multi-threaded programming
[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 ()
# Running status. See the red font. Only one CPU is used, and the system CPU idle is 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: 32953084 k total, 7391604 K used, 25561480 K free, 276480 K Buffers
Swap: 34996216 k total, 347648 K used, 34648568 K free, 3761504 K cached
PID user PR Ni virt res shr s % CPU % mem time + command
25736 root 16 0 273 m 4380 1664 s 127.6 0: 34. 65 Python
25783 root 15 0 12864 1164 R 728 2.0. 01 top
1 root 15 0 10344 564 s 528 0.0. 16 init
# Multi-CPU programming. For details, see the red font. multiple CPUs (cores/Threads) are used, and the system CPU idle is 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
Task: 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: 32953084 k total, 7407396 K used, 25545688 K free, 276568 K Buffers
Swap: 34996216 k total, 347648 K used, 34648568 K free, 3762068 K cached
PID user PR Ni virt res shr s % CPU % mem time + command
25940 root 25 0 83464 3364 R 468 100.0. 25 Python
25944 root 25 0 83464 3364 R 468 100.0. 24 Python
25945 root 25 0 83464 3364 R 468 100.0. 18 Python
25946 root 25 0 83464 3364 R 468 100.0. 24 Python
25948 root 25 0 83464 3368 R 468 100.0. 15 Python
25949 root 25 0 83464 3368 R 468 100.0. 23 Python
25951 root 25 0 83464 3372 R 468 100.0. 21 Python
25952 root 25 0 83464 3372 R 468 100.0. 23 Python
25953 root 25 0 83464 3380 R 468 100.0. 22 Python
25954 root 25 0 83464 3380 R 468 100.0. 20 Python
25943 root 25 0 83464 3364 R 468 99.6. 21 Python
25950 root 25 0 83464 3368 R 468 99.6. 15 Python
25959 root 25 0 83464 3388 R 468 50.5. 53 Python
25947 root 25 0 83464 3364 R 468 50.2. 63 Python
25957 root 25 0 83464 3384 R 468 50.2. 53 Python
25958 root 25 0 83464 3388 R 468 50.2. 56 Python
25941 root 25 0 83464 3364 R 468 49.8. 72 Python
25942 root 25 0 83464 3364 R 468 49.8. 70 Python
25955 root 25 0 83464 3384 R 468 49.8. 62 Python
25956 root 25 0 83464 3384 R 468 49.8. 61 Python
25976 root 15 0 12868 1276 R 816 0.3. 03 top
1 root 15 0 10344 564 s 528 0.0. 16 init