python多线程基本操作
進程 && 線程
進程:是內(nèi)存中的一個獨立的句柄,我們可以理解為一個應(yīng)用程序在內(nèi)存中就是一個進程。 各個進程之間是內(nèi)存相互獨立,不可共享的
線程:每個應(yīng)用運行之后就會對應(yīng)啟動一個主線程,通過主線程可以創(chuàng)建多個字線程,各個線程共享主進程的內(nèi)存空間。
關(guān)于線程、進程的解釋有一篇有趣而生動的解釋(http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html)
GIL(全局解釋器鎖)
我們知道多進程(mutilprocess) 和 多線程(threading)的目的是用來被多顆CPU進行訪問, 提高程序的執(zhí)行效率。 但是在python內(nèi)部存在一種機制(GIL),在多線程 時同一時刻只允許一個線程來訪問CPU。
GIL 并不是Python的特性,它是在實現(xiàn)Python解析器(CPython)時所引入的一個概念。就好比C++是一套語言(語法)標準,但是可以用不同的編譯器來編譯成可執(zhí)行代碼。有名的編譯器例如GCC,INTEL C++,Visual C++等。
Python也一樣,同樣一段代碼可以通過CPython,PyPy,Psyco等不同的Python執(zhí)行環(huán)境來執(zhí)行。像其中的JPython就沒有GIL。然而因為CPython是大部分環(huán)境下默認的Python執(zhí)行環(huán)境。所以在很多人的概念里CPython就是Python,也就想當然的把 GIL 歸結(jié)為Python語言的缺陷。所以這里要先明確一點:GIL并不是Python的特性,Python完全可以不依賴于GIL。
雖然python支持多線程,但是由于GIL的限制,在實際運行時,程序運行后開啟多個線程,但在通過GIL后同時也只能有一個線程被CPU執(zhí)行。
多線程
1)多線程執(zhí)行方法
def do_thread(num):
print(“this is thread %s” % str(num))
time.sleep(3)
for i in range(5):
t = Thread(target=do_thread, args=(i,))
t.start()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
以上方法就開啟了一個5個線程,target用來定義開啟線程后要執(zhí)行的方法,args為參數(shù)
線程的其它方法:
1 setName(), getName()
??setName(): ??給線程設(shè)置一個名字
??getName(): ??獲取線程的名稱
def do_thread(num):
print(“this is thread %s” % str(num))
time.sleep(3)
for i in range(2):
t = Thread(target=do_thread, args=(i,))
t.start()
t.setName(“Mythread_{0}”.format(str(i)))
print(t.getName())
run result:
this is thread 0
Mythread_0
this is thread 1
Mythread_1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2 setDaemon()
setDaemon(True/False): 設(shè)置創(chuàng)建的子線程為前臺線程或后臺線程.設(shè)置為True則子線程為后臺線程。線程默認為前臺線程(不設(shè)置此方法)
前臺線程: 當子線程創(chuàng)建完成后,主線程和子線程(前臺線程)同時運行,如果主線程執(zhí)行完成,而子線程還未完成則等待子線程執(zhí)行完成以后整個程序才結(jié)束。
后臺線程: 當子線程創(chuàng)建完成后,如果子線程還未結(jié)束,而主線程運行結(jié)束則不管子線程了,程序就結(jié)束。
此方法設(shè)置必須在 start() 方法前進行設(shè)置, 看代碼:
def do_thread(num):
print(“this is thread %s” % str(num))
time.sleep(3)
print(“OK”, str(num))
for i in range(2):
t = Thread(target=do_thread, args=(i,))
# 不設(shè)置此方法默認前臺線程,
#t.setDaemon(True)
t.setName(“Mythread_{0}”.format(str(i)))
t.start()
print(t.getName())
run result:
this is thread 0
Mythread_0
this is thread 1
Mythread_1
OK 0
OK 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
3 join()
join(timeout) : 多線程的 wait(),當主線程執(zhí)行 子線程.join() 方法后,主線程將等待子線程執(zhí)行完再接著執(zhí)行。當加上timeout參數(shù)后,如果超過timeout時間不管子線程有沒有執(zhí)行完都將結(jié)束等待
看下面兩個例子
def do_thread(num):
time.sleep(3)
print(“this is thread %s” % str(num))
for i in range(2):
t = Thread(target=do_thread, args=(i,))
t.setName(“Mythread_{0}”.format(str(i)))
t.start()
print(“print in main thread: thread name:”, t.getName())
run result:
print in main thread: thread name: Mythread_0
print in main thread: thread name: Mythread_1
this is thread 0
this is thread 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
上面無join方法時,主線程執(zhí)行完print,等待子線程函數(shù)中的print執(zhí)行完成,這個程序退出。 下面我們看看加上join方法后的效果
import time from threading import Threaddef do_thread(num):
time.sleep(3)
print(“this is thread %s” % str(num))
for i in range(2):
t = Thread(target=do_thread, args=(i,))
t.setName(“Mythread_{0}”.format(str(i)))
t.start()
t.join()
print(“print in main thread: thread name:”, t.getName())
run result:
this is thread 0
print in main thread: thread name: Mythread_0
this is thread 1
print in main thread: thread name: Mythread_1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
當程序運行到j(luò)oin后,將等待子程序執(zhí)行完成,然后才向下執(zhí)行。這樣真?zhèn)€程序就變成一個單線程的順序執(zhí)行了。多線程就沒什么鳥用了。
join()與setDaemon()都是等待子線程結(jié)束,有什么區(qū)別呢:
當執(zhí)行join()后主線程就停了,直到子線程完成后才開始接著主線程執(zhí)行,整個程序是線性的
setDaemon() 為前臺線程時,所有的線程都在同時運行,主線程也在運行。只不過是主線程運行完以后等待所有子線程結(jié)束。這個還是一個并行的執(zhí)行,執(zhí)行效率肯定要高于join()方法的。
4 線程鎖
線程是內(nèi)存共享的,當多個線程對內(nèi)存中的同一個公共變量進行操作時,會導(dǎo)致線程爭搶的問題,為了解決此問題,可以使用線程鎖。
def do_thread(num):
global public_num
# 加鎖
lock.acquire()
public_num -= 1
# 解鎖
lock.release()
time.sleep(1)
print(“public_num in thread_%s is %s” % (str(num), str(public_num)))
public_num = 100
threads_list = []
lock = threading.Lock()
for i in range(50):
t = threading.Thread(target=do_thread, args=(i,))
t.setName(“Mythread_{0}”.format(str(i)))
t.start()
threads.append(t)
# 等待所有子線程結(jié)束
for t in threads:
t.join()
print("last result of public_num is ", public_num)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
5 event()
線程的事件, 用于主線程控制子線程的執(zhí)行。它的本質(zhì)就是定義了一個全局的flag標識,并通過一些方法來獲取、設(shè)置此標識。包括:
wait()方法:當flag標識為False時,wait()方法將阻塞,為True時,wait()不阻塞
set()方法:設(shè)置flag標識為True
clear()方法: 設(shè)置flag標識為False
初始化時flag標識為False(阻塞狀態(tài))
is_set()/isSet() : 判斷當前flag標識是否為True
def do(event):
print(<span class="hljs-string">'start'</span>) <span class="hljs-comment"># 默認初始化狀態(tài)為False,到這里就阻塞了</span> event.wait() print(<span class="hljs-string">'execute\n'</span>)if name == “main”:
event_obj = threading.Event()
for i in range(10):
t = threading.Thread(target=do, args=(event_obj,))
t.start()
event一個模擬紅綠燈的實例:
def light():linght_time = 0if not event.is_set():event.set() # Flag = True, 阻塞while True:time.sleep(1)if linght_time < 10:print("Green is on....")elif linght_time < 13:print("Yellow is on ....")elif linght_time < 16:print("Red is on ......")if event.is_set():event.clear()else: # 大于16, 該重新調(diào)綠燈了linght_time = 0event.set() linght_time += <span class="hljs-number">1</span>def car_run(carnum):
while True:
time.sleep(2)
if event.is_set():
print(“car %s is run” % carnum)
else:
print(“CAR %s IS WAITTING…” % carnum)
if name == “main”:
event = threading.Event()
l = threading.Thread(target=light, )
l.start()
for i in range(3):
c = threading.Thread(target=car_run, args=(str(i), ))
c.start()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
6) Semaphore()
Semaphore信號量管理一個內(nèi)置的計數(shù)器:
每當調(diào)用acquire()時內(nèi)置計數(shù)器-1;
調(diào)用release() 時內(nèi)置計數(shù)器+1;
計數(shù)器不能小于0;當計數(shù)器為0時,acquire()將阻塞線程直到其他線程調(diào)用release()。
def do():
semaphro.acquire()
print(“this is {0} set the semaphore”.format(threading.current_thread().getName()))
time.sleep(2)
semaphro.release()
print("\033[1;30mthi is {0} release the semaphore\033[0m".format(threading.current_thread().getName()))
if name == “main”:
semaphro = threading.Semaphore(2)
for i in range(10):
t = threading.Thread(target=do)
t.setName(“Thread_{0}”.format(str(i)))
t.start()
print(“finished”)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
上例中,雖然創(chuàng)建了10個線程,但同時只有2個線程在運行,就是因為在線程中通過Semaphore設(shè)置了2個信號量。只有其中一個釋放后另其它的線程再能開始執(zhí)行
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python多线程基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cvx error
- 下一篇: websocket python爬虫_p