更新 Thread/README.md

This commit is contained in:
leo 2024-06-24 12:55:24 +08:00
parent 7dae1afca1
commit 0e4de36392

View File

@ -15,7 +15,7 @@
## 多線程 ## 多線程
### 正常使用 ### 正常使用
#### 2個線程幾乎同時啟動 #### 2個線程幾乎同時啟動
``` ```python
import threading import threading
from queue import Queue #Thread 無法回傳值,所以要使用 Queue.put() 將要傳回的值存入 Queue再用 Queue.get() 取出 from queue import Queue #Thread 無法回傳值,所以要使用 Queue.put() 將要傳回的值存入 Queue再用 Queue.get() 取出
import time import time
@ -48,7 +48,7 @@ for i in range(0,len(thread_num)):
### 非正常使用 ### 非正常使用
#### 第2個線程要等到第1個線程運行完才會啟動 #### 第2個線程要等到第1個線程運行完才會啟動
##### 失去多線程意義 ##### 失去多線程意義
``` ```python
# A報數 # A報數
def A_Count_off(): def A_Count_off():
for i in range(0,5): for i in range(0,5):
@ -74,7 +74,7 @@ for i in range(0,len(thread_num)):
``` ```
### 攜帶參數 ### 攜帶參數
``` ```python
def Count_off(code_name): def Count_off(code_name):
print('process {} '.format(os.getpid())) print('process {} '.format(os.getpid()))
print('thread {} '.format(threading.current_thread().name)) print('thread {} '.format(threading.current_thread().name))
@ -99,7 +99,7 @@ for i in range(0,len(thread_list)):
#### 後續觀看程式碼及維護,會提高不少 #### 後續觀看程式碼及維護,會提高不少
* start會使用分出去的線程 * start會使用分出去的線程
* run會使用主線程 * run會使用主線程
``` ```python
class Thread_class(threading.Thread): class Thread_class(threading.Thread):
def __init__(self,code_name): def __init__(self,code_name):
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -124,7 +124,7 @@ for i in range(0,len(thread_list)):
``` ```
``` ```python
class Thread_class(threading.Thread): class Thread_class(threading.Thread):
def __init__(self,code_name): def __init__(self,code_name):
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -152,7 +152,7 @@ for i in range(0,len(thread_list)):
* 使用方式與thread相似 * 使用方式與thread相似
* 多了信號槽使用(可以擁有多個) * 多了信號槽使用(可以擁有多個)
* 方便與主UI的線程溝通 * 方便與主UI的線程溝通
``` ```python
class ReadTime(QtCore.QThread): # 讀取時間 class ReadTime(QtCore.QThread): # 讀取時間
time_out = pyqtSignal(str) # 聲明一個帶字串參數的信號槽 time_out = pyqtSignal(str) # 聲明一個帶字串參數的信號槽
def __init__(self, parent=None): def __init__(self, parent=None):
@ -168,7 +168,7 @@ class ReadTime(QtCore.QThread): # 讀取時間
### 多進程與多線程的寫法極為相似 ### 多進程與多線程的寫法極為相似
#### 正常使用: #### 正常使用:
#### 2個進程幾乎同時啟動 #### 2個進程幾乎同時啟動
``` ```python
# A報數 # A報數
def A_Count_off(): def A_Count_off():
for i in range(0,5): for i in range(0,5):
@ -199,7 +199,7 @@ if __name__=='__main__':
#### 第2個進程要等到第1個進程運行完才會啟動 #### 第2個進程要等到第1個進程運行完才會啟動
#### 失去多進程意義 #### 失去多進程意義
``` ```python
# A報數 # A報數
def A_Count_off(): def A_Count_off():
for i in range(0,5): for i in range(0,5):
@ -228,7 +228,7 @@ if __name__=='__main__':
``` ```
### 攜帶參數 ### 攜帶參數
``` ```python
if __name__=='__main__': if __name__=='__main__':
process_name_list=["A","B","C","D"] process_name_list=["A","B","C","D"]
process_list=[] process_list=[]
@ -240,7 +240,7 @@ if __name__=='__main__':
* start會使用分出去的進程 * start會使用分出去的進程
* run會使用主線程 * run會使用主線程
* 但start後無法直接調用此class的參數等等會大概說明 * 但start後無法直接調用此class的參數等等會大概說明
``` ```python
class Process_class(mp.Process): class Process_class(mp.Process):
def __init__(self, code_name): def __init__(self, code_name):
mp.Process.__init__(self) mp.Process.__init__(self)