# 多線程和多進程 - 初窺

## 二、舉例

### 1. 多線程、多進程在操作系統中的表現形式

【test1.py】 -- 多線程

``````import threading

def foo():
while 1:
pass

``````

【test2.py】 -- 多進程

``````import multiprocessing

def foo():
while 1:
pass

``````

### 2. 單線程、多線程、多進程的運行速度

【test3.py】 -- 單線程

``````import time

N = 100000000

def foo(n):
while n > 0:
n -= 1

start = time.time()
foo(N)
end = time.time()
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test3.py
Time taken in seconds: 2.91
``````

【test4.py】 -- 多線程

``````import time

N = 100000000

def foo(n):
while n > 0:
n -= 1

start = time.time()
end = time.time()
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test4.py
Time taken in seconds: 6.0
``````

【test5.py】 -- 多進程

``````import time
import multiprocessing

N = 100000000

def foo(n):
while n > 0:
n -= 1

start = time.time()
end = time.time()
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test5.py
Time taken in seconds: 1.48
``````

### 3. 單線程、多線程、多進程的執行結果

【test6.py】 -- 單線程

``````import time

N = 10000000
sum = 0

def foo(n):
global sum
for i in range(0, n):
sum += 1

start = time.time()
foo(N)
end = time.time()

print('The value of sum: {}'.format(sum))
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test6.py
The value of sum: 10000000
Time taken in seconds: 1.26
``````

【test7.py】 -- 多線程

``````import time

N = 10000000
sum = 0

def foo(n):
global sum
for i in range(0, n):
sum += 1

start = time.time()
end = time.time()

print('The value of sum: {}'.format(sum))
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test7.py
The value of sum: 7333348
Time taken in seconds: 1.76
``````

【test8.py】 -- 多進程

``````import time
import multiprocessing

N = 10000000
sum = 0

def foo(n):
global sum
for i in range(0, n):
sum += 1

start = time.time()
end = time.time()

print('The value of sum: {}'.format(sum))
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test8.py
The value of sum: 0
Time taken in seconds: 0.57
``````

【test9.py】 -- 多線程

``````import time

N = 10000000
sum = 0

def foo(n):
global sum
global lock
for i in range(0, n):
with lock:
sum += 1

start = time.time()
end = time.time()

print('The value of sum: {}'.format(sum))
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test9.py
The value of sum: 10000000
Time taken in seconds: 21.49
``````

【test10.py】 -- 多進程

``````import time
import multiprocessing

N = 10000000
sum = multiprocessing.Value('i', 0)
lock = multiprocessing.Lock()

def foo(n):
global sum
global lock
for i in range(0, n):
with lock:
sum.value += 1

start = time.time()
end = time.time()

print('The value of sum: {}'.format(sum.value))
print('Time taken in seconds: {}'.format(round(end-start, 2)))
``````
``````[root@34host ~]# python test10.py
The value of sum: 10000000
Time taken in seconds: 41.3
``````

## 三、問題

1. 多線程跟多進程有什麼區別，什麼時候用多線程，什麼時候用多進程？
2. 單線程、多線程、多進程的效率問題？
3. 多線程、多進程在編程的時候有哪些注意事項？