多線程類似于同時執(zhí)行多個不同程序,多線程運行有如下優(yōu)點:
- 使用線程可以把占據(jù)長時間的程序中的任務(wù)放到后臺去處理。
- 用戶界面可以更加吸引人,比如用戶點擊了一個按鈕去觸發(fā)某些事件的處理,可以彈出一個進度條來顯示處理的進度。
- 程序的運行速度可能加快。
- 在一些等待的任務(wù)實現(xiàn)上如用戶輸入、文件讀寫和網(wǎng)絡(luò)收發(fā)數(shù)據(jù)等,線程就比較有用了。在這種情況下我們可以釋放一些珍貴的資源如內(nèi)存占用等等。
每個獨立的線程有一個程序運行的入口、順序執(zhí)行序列和程序的出口。但是線程不能夠獨立執(zhí)行,必須依存在應(yīng)用程序中,由應(yīng)用程序提供多個線程執(zhí)行控制。
每個線程都有他自己的一組CPU寄存器,稱為線程的上下文,該上下文反映了線程上次運行該線程的CPU寄存器的狀態(tài)。
指令指針和堆棧指針寄存器是線程上下文中兩個最重要的寄存器,線程總是在進程得到上下文中運行的,這些地址都用于標志擁有線程的進程地址空間中的內(nèi)存。
- 線程可以被搶占(中斷)。
- 在其他線程正在運行時,線程可以暫時擱置(也稱為睡眠) -- 這就是線程的退讓。
線程可以分為:
- 內(nèi)核線程: 由操作系統(tǒng)內(nèi)核創(chuàng)建和撤銷。
- 用戶線程: 不需要內(nèi)核支持而在用戶程序中實現(xiàn)的線程。
Python3 線程中常用的兩個模塊為:
- _thread
- threading(推薦使用)
thread 模塊已被廢棄。用戶可以使用 threading 模塊代替。所以,在 Python3 中不能再使用"thread" 模塊。為了兼容性,Python3 將 thread 重命名為 "_thread"。
threading 模塊除了包含 _thread 模塊中的所有方法外,還提供的其他方法:
- threading.currentThread(): 返回當前的線程變量。
- threading.enumerate(): 返回一個包含正在運行的線程的list。正在運行指線程啟動后、結(jié)束前,不包括啟動前和終止后的線程。
- threading.activeCount(): 返回正在運行的線程數(shù)量,與len(threading.enumerate())有相同的結(jié)果。
除了使用方法外,線程模塊同樣提供了Thread類來處理線程,Thread類提供了以下方法:
- run(): ?用以表示線程活動的方法。
-
start():
啟動線程活動。
?
- join([time]): ?等待至線程中止。這阻塞調(diào)用線程直至線程的join() 方法被調(diào)用中止-正常退出或者拋出未處理的異常-或者是可選的超時發(fā)生。
- isAlive(): ?返回線程是否活動的。
- getName(): ?返回線程名。
- setName(): ?設(shè)置線程名。
舉個例子:
1
#
!/usr/bin/python3
2
#
3birds coypright
3
4
import
threading
5
import
time
6
7
exitFlag =
0
8
9
class
myThread (threading.Thread):
10
def
__init__
(self, threadID, name, counter):
11
threading.Thread.
__init__
(self)
12
self.threadIN =
threadID
13
self.name =
name
14
self.counter =
counter
15
def
run(self):
16
print
(
"
3birds start thread:
"
+
self.name)
17
threadLock.acquire()
18
print_time(self.name, self.counter, 5
)
19
threadLock.release()
20
print
(
"
3birds exit thread:
"
+
self.name)
21
22
def
print_time(threadName, delay, counter):
23
while
counter:
24
if
exitFlag:
25
threadName.exit()
26
time.sleep(delay)
27
print
(
"
%s: %s
"
%
(threadName, time.ctime(time.time())))
28
counter -= 1
29
30
threadLock =
threading.Lock()
31
threads =
[]
32
33
thread1 = myThread(1,
"
Thread-1
"
, 1
)
34
thread2 = myThread(2,
"
Thread-2
"
, 2
)
35
36
#
start new thread
37
thread1.start()
38
thread2.start()
39
thread1.join()
40
thread2.join()
41
print
(
"
end the thread
"
)
運行結(jié)果:
3birds start thread:Thread-1
3birds start thread:Thread
-2
Thread
-1: Tue Jul 23 09:08:46 2019
Thread
-1: Tue Jul 23 09:08:47 2019
Thread
-1: Tue Jul 23 09:08:48 2019
Thread
-1: Tue Jul 23 09:08:49 2019
Thread
-1: Tue Jul 23 09:08:50 2019
3birds exit thread:Thread
-1
Thread
-2: Tue Jul 23 09:08:52 2019
Thread
-2: Tue Jul 23 09:08:54 2019
Thread
-2: Tue Jul 23 09:08:56 2019
Thread
-2: Tue Jul 23 09:08:58 2019
Thread
-2: Tue Jul 23 09:09:00 2019
3birds exit thread:Thread
-2
end the thread
參考文檔:
1?http://www.runoob.com/python3/python3-multithreading.html
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

