1. 程式人生 > >Python網絡編程之線程與進程

Python網絡編程之線程與進程

楊文 python

What is a Thread?

線程是操作系統能夠進行運算調度的最小單位,它被包含在進程之中,是進程中的實際運作單位,一條線程指的是進程中一個單一順序的控制流,一個進程中可以並發多個線程,每條線程並行執行不同的任務。

在同一個進程內的線程的數據是可以進行互相訪問的。

線程的切換使用過上下文來實現的,比如有一本書,有a和b這兩個人(兩個線程)看,a看完之後記錄當前看到那一頁哪一行,然後交給b看,b看完之後記錄當前看到了那一頁哪一行,此時a又要看了,那麽a就通過上次記錄的值(上下文)直接找到上次看到了哪裏,然後繼續往下看。



What is a Process?

一個進程至少要包含一個線程,每個進程在啟動的時候就會自動的啟動一個線程,進程裏面的第一個線程就是主線程,每次在進程內創建的子線程都是由主線程進程創建和銷毀,子線程也可以由主線程創建出來的線程創建和銷毀線程。

進程是對各種資源管理的集合,比如要調用內存、CPU、網卡、聲卡等,進程要操作上述的硬件之前都必須要創建一個線程,進程裏面可以包含多個線程,QQ就是一個進程。

繼續拿QQ來說,比如我現在打卡了QQ的聊天窗口、個人信息窗口、設置窗口等,那麽每一個打開的窗口都是一個線程,他們都在執行不同的任務,比如聊天窗口這個線程可以和好友進行互動,聊天,視頻等,個人信息窗口我可以查看、修改自己的資料。

為了進程安全起見,所以兩個進程之間的數據是不能夠互相訪問的(默認情況下),比如自己寫了一個應用程序,然後讓別人運行起來,那麽我的這個程序就可以訪問用戶啟動的其他應用,我可以通過我自己的程序去訪問QQ,然後拿到一些聊天記錄等比較隱秘的信息,那麽這個時候就不安全了,所以說進程與進程之間的數據是不可以互相訪問的,而且每一個進程的內存是獨立的。


進程與線程的區別?

  1. 線程是執行的指令集,進程是資源的集合

  2. 線程的啟動速度要比進程的啟動速度要快

  3. 兩個線程的執行速度是一樣的

  4. 進程與線程的運行速度是沒有可比性的

  5. 線程共享創建它的進程的內存空間,進程的內存是獨立的。

  6. 兩個線程共享的數據都是同一份數據,兩個子進程的數據不是共享的,而且數據是獨立的;

  7. 同一個進程的線程之間可以直接交流,同一個主進程的多個子進程之間是不可以進行交流,如果兩個進程之間需要通信,就必須要通過一個中間代理來實現;

  8. 一個新的線程很容易被創建,一個新的進程創建需要對父進程進行一次克隆

  9. 一個線程可以控制和操作同一個進程裏的其他線程,線程與線程之間沒有隸屬關系,但是進程只能操作子進程

  10. 改變主線程,有可能會影響到其他線程的行為,但是對於父進程的修改是不會影響子進程;

一個多並發的小腳本

  1. import threading

  2. import time

  3. def Princ(String):

  4. print(‘task‘, String)

  5. time.sleep(5)

  6. # target=目標函數, args=傳入的參數

  7. t1 = threading.Thread(target=Princ, args=(‘t1‘,))

  8. t1.start()

  9. t2 = threading.Thread(target=Princ, args=(‘t1‘,))

  10. t2.start()

  11. t3 = threading.Thread(target=Princ, args=(‘t1‘,))

  12. t3.start()

參考文檔

進程與線程的一個簡單解釋
http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
Linux進程與線程的區別
https://my.oschina.net/cnyinlinux/blog/422207

線程

Thread module emulating a subset of Java’s threading model.

調用threading模塊調用線程的兩種方式

直接調用

  1. import threading

  2. import time

  3. def Princ(String):

  4. print(‘task‘, String)

  5. time.sleep(5)

  6. # target=目標函數, args=傳入的參數

  7. t1 = threading.Thread(target=Princ, args=(‘t1‘,))

  8. t1.start()

  9. t2 = threading.Thread(target=Princ, args=(‘t1‘,))

  10. t2.start()

  11. t3 = threading.Thread(target=Princ, args=(‘t1‘,))

  12. t3.start()

通過類調用

  1. import threading

  2. import time

  3. class MyThreading(threading.Thread):

  4. def __init__(self, conn):

  5. super(MyThreading, self).__init__()

  6. self.conn = conn

  7. def run(self):

  8. print(‘run task‘, self.conn)

  9. time.sleep(5)

  10. t1 = MyThreading(‘t1‘)

  11. t2 = MyThreading(‘t2‘)

  12. t1.start()

  13. t2.start()

多線程

多線程在Python內實則就是一個假象,為什麽這麽說呢,因為CPU的處理速度是很快的,所以我們看起來以一個線程在執行多個任務,每個任務的執行速度是非常之快的,利用上下文切換來快速的切換任務,以至於我們根本感覺不到。

但是頻繁的使用上下文切換也是要耗費一定的資源,因為單線程在每次切換任務的時候需要保存當前任務的上下文。

什麽時候用到多線程?

首先IO操作是不占用CPU的,只有計算的時候才會占用CPU(譬如1+1=2),Python中的多線程不適合CPU密集型的任務,適合IO密集型的任務(sockt server)。

啟動多個線程

主進程在啟動之後會啟動一個主線程,下面的腳本中讓主線程啟動了多個子線程,然而啟動的子線程是獨立的,所以主線程不會等待子線程執行完畢,而是主線程繼續往下執行,並行執行。

  1. for i in range(50):

  2. t = threading.Thread(target=Princ, args=(‘t-%s‘ % (i),))

  3. t.start()

join()

join()方法可以讓程序等待每一個線程之後完成之後再往下執行,又成為串行執行。

  1. import threading

  2. import time

  3. def Princ(String):

  4. print(‘task‘, String)

  5. time.sleep(1)

  6. for i in range(50):

  7. t = threading.Thread(target=Princ, args=(‘t-%s‘ % (i),))

  8. t.start()

  9. # 當前線程執行完畢之後在執行後面的線程

  10. t.join()

讓主線程阻塞,子現在並行執行

  1. import threading

  2. import time

  3. def Princ(String):

  4. print(‘task‘, String)

  5. time.sleep(2)

  6. # 執行子線程的時間

  7. start_time = time.time()

  8. # 存放線程的實例

  9. t_objs = []

  10. for i in range(50):

  11. t = threading.Thread(target=Princ, args=(‘t-%s‘ % (i),))

  12. t.start()

  13. # 為了不讓後面的子線程阻塞,把當前的子線程放入到一個列表中

  14. t_objs.append(t)

  15. # 循環所有子線程實例,等待所有子線程執行完畢

  16. for t in t_objs:

  17. t.join()

  18. # 當前時間減去開始時間就等於執行的過程中需要的時間

  19. print(time.time() - start_time)

查看主線程與子線程

  1. import threading

  2. class MyThreading(threading.Thread):

  3. def __init__(self):

  4. super(MyThreading, self).__init__()

  5. def run(self):

  6. print(‘我是子線程: ‘, threading.current_thread())

  7. t = MyThreading()

  8. t.start()

  9. print(‘我是主線程: ‘, threading.current_thread())

輸出如下:

  1. C:\Python\Python35\python.exe E:/MyCodeProjects/進程與線程/s3.py

  2. 我是子線程: <MyThreading(Thread-1, started 7724)>

  3. 我是主線程: <_MainThread(MainThread, started 3680)>

  4. Process finished with exit code 0

查看當前進程的活動線程個數

  1. import threading

  2. class MyThreading(threading.Thread):

  3. def __init__(self):

  4. super(MyThreading, self).__init__()

  5. def run(self):

  6. print(‘www.anshengme.com‘)

  7. t = MyThreading()

  8. t.start()

  9. print(‘線程個數: ‘, threading.active_count())

輸出如下:

  1. C:\Python\Python35\python.exe E:/MyCodeProjects/進程與線程/s3.py

  2. www.anshengme.com

  3. # 一個主線程和一個子線程

  4. 線程個數: 2

  5. Process finished with exit code 0

Event

Event是線程間通信最間的機制之一:一個線程發送一個event信號,其他的線程則等待這個信號。用於主線程控制其他線程的執行。 Events 管理一個flag,這個flag可以使用set
()設置成True或者使用clear()重置為False,wait()則用於阻塞,在flag為True之前。flag默認為False。

選項描述
Event.wait([timeout])堵塞線程,直到Event對象內部標識位被設為True或超時(如果提供了參數timeout)
Event.set()將標識位設為Ture
Event.clear()將標識伴設為False
Event.isSet()判斷標識位是否為Ture
  1. #!/use/bin/env python

  2. # _*_ coding: utf-8- _*_

  3. import threading

  4. def runthreading(event):

  5. print("Start...")

  6. event.wait()

  7. print("End...")

  8. event_obj = threading.Event()

  9. for n in range(10):

  10. t = threading.Thread(target=runthreading, args=(event_obj,))

  11. t.start()

  12. event_obj.clear()

  13. inp = input("True/False?>> ")

  14. if inp == "True":

  15. event_obj.set()

  16. `

守護進程(守護線程)

一個主進程可以啟動多個守護進程,但是主進程必須要一直運行,如果主進程掛掉了,那麽守護進程也會隨之掛掉

程序會等待主線程(進程)執行完畢,但是不會等待守護進程(線程)

  1. import threading

  2. import time

  3. def Princ(String):

  4. print(‘task‘, String)

  5. time.sleep(2)

  6. for i in range(50):

  7. t = threading.Thread(target=Princ, args=(‘t-%s‘ % (i),))

  8. t.setDaemon(True) # 把當前線程設置為守護線程,要在start之前設置

  9. t.start()

場景預設: 比如現在有一個FTP服務,每一個用戶連接上去的時候都會創建一個守護線程,現在已經有300個用戶連接上去了,就是說已經創建了300個守護線程,但是突然之間FTP服務宕掉了,這個時候就不會等待守護線程執行完畢再退出,而是直接退出,如果是普通的線程,那麽就會登臺線程執行完畢再退出。

  1. #!/use/bin/env python

  2. # _*_ coding:utf-8 _*_

  3. from multiprocessing import Process

  4. import time

  5. def runprocess(arg):

  6. print(arg)

  7. time.sleep(2)

  8. p = Process(target=runprocess, args=(11,))

  9. p.daemon=True

  10. p.start()

  11. print("end")

線程之間的數據交互與鎖(互斥鎖)

python2.x需要加鎖,但是在python3.x上面就不需要了

  1. # _*_ coding:utf-8 _*_

  2. import threading

  3. def Princ():

  4. # 獲取鎖

  5. lock.acquire()

  6. # 在函數內可以直接修改全局變量

  7. global number

  8. number += 1

  9. # 為了避免讓程序出現串行,不能加sleep

  10. # time.sleep(1)

  11. # 釋放鎖

  12. lock.release()

  13. # 鎖

  14. lock = threading.Lock()

  15. # 主線程的number

  16. number = 0

  17. t_objs = []

  18. for i in range(100):

  19. t = threading.Thread(target=Princ)

  20. t.start()

  21. t_objs.append(t)

  22. for t in t_objs:

  23. t.join()

  24. print(‘Number:‘, number)

遞歸鎖(Lock/RLock)

  1. import threading

  2. def run1():

  3. print("grab the first part data")

  4. lock.acquire()

  5. global num

  6. num += 1

  7. lock.release()

  8. return num

  9. def run2():

  10. print("grab the second part data")

  11. lock.acquire()

  12. global num2

  13. num2 += 1

  14. lock.release()

  15. return num2

  16. def run3():

  17. lock.acquire()

  18. res = run1()

  19. print(‘--------between run1 and run2-----‘)

  20. res2 = run2()

  21. lock.release()

  22. print(res, res2)

  23. t_objs = []

  24. if __name__ == ‘__main__‘:

  25. num, num2 = 0, 0

  26. lock = threading.RLock() # RLock()類似創建了一個字典,每次退出的時候找到字典的值進行退出

  27. # lock = threading.Lock() # Lock()會阻塞在這兒

  28. for i in range(10):

  29. t = threading.Thread(target=run3)

  30. t.start()

  31. t_objs.append(t)

  32. for t in t_objs:

  33. t.join()

  34. print(num, num2)

信號量(Semaphore)

互斥鎖同時只允許一個線程更改數據,而Semaphore是同時允許一定數量的線程更改數據

  1. import threading

  2. import time

  3. def run(n):

  4. semaphore.acquire() # 獲取信號,信號可以有多把鎖

  5. time.sleep(1) # 等待一秒鐘

  6. print("run the thread: %s\n" % n)

  7. semaphore.release() # 釋放信號

  8. t_objs = []

  9. if __name__ == ‘__main__‘:

  10. semaphore = threading.BoundedSemaphore(5) # 聲明一個信號量,最多允許5個線程同時運行

  11. for i in range(20): # 運行20個線程

  12. t = threading.Thread(target=run, args=(i,)) # 創建線程

  13. t.start() # 啟動線程

  14. t_objs.append(t)

  15. for t in t_objs:

  16. t.join()

  17. print(‘>>>>>>>>>>>>>‘)

以上代碼中,類似與創建了一個隊列,最多放5個任務,每執行完成一個任務就會往後面增加一個任務。

多進程

多進程的資源是獨立的,不可以互相訪問。

啟動一個進程

  1. from multiprocessing import Process

  2. import time

  3. def f(name):

  4. time.sleep(2)

  5. print(‘hello‘, name)

  6. if __name__ == ‘__main__‘:

  7. # 創建一個進程

  8. p = Process(target=f, args=(‘bob‘,))

  9. # 啟動

  10. p.start()

  11. # 等待進程執行完畢

  12. p.join(

在進程內啟動一個線程

  1. from multiprocessing import Process

  2. import threading

  3. def Thread(String):

  4. print(String)

  5. def Proces(String):

  6. print(‘hello‘, String)

  7. t = threading.Thread(target=Thread, args=(‘Thread %s‘ % (String),)) # 創建一個線程

  8. t.start() # 啟動它

  9. if __name__ == ‘__main__‘:

  10. p = Process(target=Proces, args=(‘World‘,)) # 創建一個進程

  11. p.start() # 啟動

  12. p.join() # 等待進程執行完畢

啟動一個多進程

  1. from multiprocessing import Process

  2. import time

  3. def f(name):

  4. time.sleep(2)

  5. print(‘hello‘, name)

  6. if __name__ == ‘__main__‘:

  7. for n in range(10): # 創建一個進程

  8. p = Process(target=f, args=(‘bob %s‘ % (n),))

  9. # 啟動

  10. p.start()

  11. # 等待進程執行完畢

獲取啟動進程的PID

  1. # _*_ coding:utf-8 _*_

  2. from multiprocessing import Process

  3. import os

  4. def info(String):

  5. print(String)

  6. print(‘module name:‘, __name__)

  7. print(‘父進程的PID:‘, os.getppid())

  8. print(‘子進程的PID:‘, os.getpid())

  9. print("\n")

  10. def ChildProcess():

  11. info(‘\033[31;1mChildProcess\033[0m‘)

  12. if __name__ == ‘__main__‘:

  13. info(‘\033[32;1mTheParentProcess\033[0m‘)

  14. p = Process(target=ChildProcess)

  15. p.start()

輸出結果

  1. C:\Python\Python35\python.exe E:/MyCodeProjects/多進程/s1.py

  2. TheParentProcess

  3. module name: __main__

  4. # Pycharm的PID

  5. 父進程的PID: 6888

  6. # 啟動的腳本PID

  7. 子進程的PID: 4660

  8. ChildProcess

  9. module name: __mp_main__

  10. # 腳本的PID

  11. 父進程的PID: 4660

  12. # 父進程啟動的子進程PID

  13. 子進程的PID: 8452

  14. Process finished with exit code 0

進程間通信

默認情況下進程與進程之間是不可以互相通信的,若要實現互相通信則需要一個中間件,另個進程之間通過中間件來實現通信,下面是進程間通信的幾種方式。

進程Queue

  1. # _*_ coding:utf-8 _*_

  2. from multiprocessing import Process, Queue

  3. def ChildProcess(Q):

  4. Q.put([‘Hello‘, None, ‘World‘]) # 在Queue裏面上傳一個列表

  5. if __name__ == ‘__main__‘:

  6. q = Queue() # 創建一個Queue

  7. p = Process(target=ChildProcess, args=(q,)) # 創建一個子進程,並把Queue傳給子進程,相當於克隆了一份Queue

  8. p.start() # 啟動子進程

  9. print(q.get()) # 獲取q中的數據

  10. p.join()

管道(Pipes)

  1. # _*_ coding:utf-8 _*_

  2. from multiprocessing import Process, Pipe

  3. def ChildProcess(conn):

  4. conn.send([‘Hello‘, None, ‘World‘]) # 寫一段數據

  5. conn.close() # 關閉

  6. if __name__ == ‘__main__‘:

  7. parent_conn, child_conn = Pipe() # 生成一個管道實例,parent_conn, child_conn管道的兩頭

  8. p = Process(target=ChildProcess, args=(child_conn,))

  9. p.start()

  10. print(parent_conn.recv()) # 收取消息

  11. p.join()

數據共享(Managers)

  1. # _*_ coding:utf-8 _*_

  2. # _*_ coding:utf-8 _*_

  3. from multiprocessing import Process, Manager

  4. import os

  5. def ChildProcess(Dict, List):

  6. Dict[‘k1‘] = ‘v1‘

  7. Dict[‘k2‘] = ‘v2‘

  8. List.append(os.getpid()) # 獲取子進程的PID

  9. print(List) # 輸出列表中的內容

  10. if __name__ == ‘__main__‘:

  11. manager = Manager() # 生成Manager對象

  12. Dict = manager.dict() # 生成一個可以在多個進程之間傳遞共享的字典

  13. List = manager.list() # 生成一個字典

  14. ProcessList = [] # 創建一個空列表,存放進程的對象,等待子進程執行用於

  15. for i in range(10): # 生成是個子進程

  16. p = Process(target=ChildProcess, args=(Dict, List)) # 創建一個子進程

  17. p.start() # 啟動

  18. ProcessList.append(p) # 把子進程添加到p_list列表中

  19. for res in ProcessList: # 循環所有的子進程

  20. res.join() # 等待執行完畢

  21. print(‘\n‘)

  22. print(Dict)

  23. print(List)

輸出結果

  1. C:\Python\Python35\python.exe E:/MyCodeProjects/多進程/s4.py

  2. [5112]

  3. [5112, 3448]

  4. [5112, 3448, 4584]

  5. [5112, 3448, 4584, 2128]

  6. [5112, 3448, 4584, 2128, 11124]

  7. [5112, 3448, 4584, 2128, 11124, 10628]

  8. [5112, 3448, 4584, 2128, 11124, 10628, 5512]

  9. [5112, 3448, 4584, 2128, 11124, 10628, 5512, 10460]

  10. [5112, 3448, 4584, 2128, 11124, 10628, 5512, 10460, 10484]

  11. [5112, 3448, 4584, 2128, 11124, 10628, 5512, 10460, 10484, 6804]

  12. {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘}

  13. [5112, 3448, 4584, 2128, 11124, 10628, 5512, 10460, 10484, 6804]

  14. Process finished with exit code 0

鎖(Lock)

  1. from multiprocessing import Process, Lock

  2. def ChildProcess(l, i):

  3. l.acquire() # 獲取鎖

  4. print(‘hello world‘, i)

  5. l.release() # 釋放鎖

  6. if __name__ == ‘__main__‘:

  7. lock = Lock() # 生成Lock對象

  8. for num in range(10):

  9. Process(target=ChildProcess, args=(lock, num)).start() # 創建並啟動一個子進程

進程池

同一時間啟動多少個進程

  1. #!/use/bin/env python

  2. # _*_ coding: utf-8 _*_

  3. from multiprocessing import Pool

  4. import time

  5. def myFun(i):

  6. time.sleep(2)

  7. return i+100

  8. def end_call(arg):

  9. print("end_call>>", arg)

  10. p = Pool(5) # 允許進程池內同時放入5個進程

  11. for i in range(10):

  12. p.apply_async(func=myFun, args=(i,),callback=end_call) # # 平行執行,callback是主進程來調用

  13. # p.apply(func=Foo) # 串行執行

  14. print("end")

  15. p.close()

  16. p.join() # 進程池中進程執行完畢後再關閉,如果註釋,那麽程序直接關閉。

線程池

簡單實現

  1. #!/usr/bin/env python

  2. # -*- coding:utf-8 -*-

  3. import threading

  4. import queue

  5. import time

  6. class MyThread:

  7. def __init__(self,max_num=10):

  8. self.queue = queue.Queue()

  9. for n in range(max_num):

  10. self.queue.put(threading.Thread)

  11. def get_thread(self):

  12. return self.queue.get()

  13. def put_thread(self):

  14. self.queue.put(threading.Thread)

  15. pool = MyThread(5)

  16. def RunThread(arg,pool):

  17. print(arg)

  18. time.sleep(2)

  19. pool.put_thread()

  20. for n in range(30):

  21. thread = pool.get_thread()

  22. t = thread(target=RunThread, args=(n,pool,))

  23. t.start()

復雜版本

  1. #!/usr/bin/env python

  2. # -*- coding:utf-8 -*-

  3. import queue

  4. import threading

  5. import contextlib

  6. import time

  7. StopEvent = object()

  8. class ThreadPool(object):

  9. def __init__(self, max_num, max_task_num = None):

  10. if max_task_num:

  11. self.q = queue.Queue(max_task_num)

  12. else:

  13. self.q = queue.Queue()

  14. self.max_num = max_num

  15. self.cancel = False

  16. self.terminal = False

  17. self.generate_list = []

  18. self.free_list = []

  19. def run(self, func, args, callback=None):

  20. """

  21. 線程池執行一個任務

  22. :param func: 任務函數

  23. :param args: 任務函數所需參數

  24. :param callback: 任務執行失敗或成功後執行的回調函數,回調函數有兩個參數1、任務函數執行狀態;2、任務函數返回值(默認為None,即:不執行回調函數)

  25. :return: 如果線程池已經終止,則返回True否則None

  26. """

  27. if self.cancel:

  28. return

  29. if len(self.free_list) == 0 and len(self.generate_list) < self.max_num:

  30. self.generate_thread()

  31. w = (func, args, callback,)

  32. self.q.put(w)

  33. def generate_thread(self):

  34. """

  35. 創建一個線程

  36. """

  37. t = threading.Thread(target=self.call)

  38. t.start()

  39. def call(self):

  40. """

  41. 循環去獲取任務函數並執行任務函數

  42. """

  43. current_thread = threading.currentThread()

  44. self.generate_list.append(current_thread)

  45. event = self.q.get()

  46. while event != StopEvent:

  47. func, arguments, callback = event

  48. try:

  49. result = func(*arguments)

  50. success = True

  51. except Exception as e:

  52. success = False

  53. result = None

  54. if callback is not None:

  55. try:

  56. callback(success, result)

  57. except Exception as e:

  58. pass

  59. with self.worker_state(self.free_list, current_thread):

  60. if self.terminal:

  61. event = StopEvent

  62. else:

  63. event = self.q.get()

  64. else:

  65. self.generate_list.remove(current_thread)

  66. def close(self):

  67. """

  68. 執行完所有的任務後,所有線程停止

  69. """

  70. self.cancel = True

  71. full_size = len(self.generate_list)

  72. while full_size:

  73. self.q.put(StopEvent)

  74. full_size -= 1

  75. def terminate(self):

  76. """

  77. 無論是否還有任務,終止線程

  78. """

  79. self.terminal = True

  80. while self.generate_list:

  81. self.q.put(StopEvent)

  82. self.q.queue.clear()

  83. @contextlib.contextmanager

  84. def worker_state(self, state_list, worker_thread):

  85. """

  86. 用於記錄線程中正在等待的線程數

  87. """

  88. state_list.append(worker_thread)

  89. try:

  90. yield

  91. finally:

  92. state_list.remove(worker_thread)

  93. # How to use

  94. pool = ThreadPool(5)

  95. def callback(status, result):

  96. # status, execute action status

  97. # result, execute action return value

  98. pass

  99. def action(i):

  100. print(i)

  101. for i in range(30):

  102. ret = pool.run(action, (i,), callback)

  103. time.sleep(5)

  104. print(len(pool.generate_list), len(pool.free_list))

  105. print(len(pool.generate_list), len(pool.free_list))

  106. pool.close()

  107. pool.terminate()

什麽是IO密集型和CPU密集型?

IO密集型(I/O bound)

頻繁網絡傳輸、讀取硬盤及其他IO設備稱之為IO密集型,最簡單的就是硬盤存取數據,IO操作並不會涉及到CPU。

計算密集型(CPU bound)

程序大部分在做計算、邏輯判斷、循環導致cpu占用率很高的情況,稱之為計算密集型,比如說python程序中執行了一段代碼1+1,這就是在計算1+1的值


本文出自 “一盞燭光” 博客,謝絕轉載!

Python網絡編程之線程與進程