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