input
的一個進程內的全部線程之間的數據是共享的數據庫
#啓動多線程 from threading import Thread import time def func(i): time.sleep(1) print(i) for i in range(10): t = Thread(target=func, args=(i,)) t.start() print('這是主線程執行的') #結果 這是主線程執行的 0 2 1 3 4 5 9 8 6 7 #這裏的十個數字是幾乎同時被輸出,說明是個線程併發
面向對象的形式啓動線程網絡
#用面向對象的方法開啓新的線程 from threading import Thread class MyThread(Thread): #繼承Thread類 def __init__(self, arg): #重寫__init__方法,用於給這個類傳參 super().__init__() #繼承父類的__init__方法 self.arg = arg #將本身的參數賦給對象 def run(self): '''objece.start()直接調用這個方法''' self.methond() def methond(self): '''這個類中的其餘方法''' print(self.arg)
global n
線程共享from multiprocessing import Process from threading import Thread import time def cal_num(i): i += 1 if __name__ == '__main__': p_list = [] start = time.time() for i in range(100): #建立100個進程執行計算 p = Process(target=cal_num, args=(i, )) p.start() p_list.append(p) for i in p_list: i.join() end = time.time() t1 = end - start start = time.time() t_list = [] for i in range(100): t = Thread(target=cal_num, args=(i,)) t.start() t_list.append(t) for i in t_list: i.join() end = time.time() t2 = end - start print('100個進程消耗時間{} \n 100個線程消耗時間{}'.format(t1, t2)) #結果 100個進程消耗時間0.19627618789672852 100個線程消耗時間0.009418964385986328
threading.current_thread()
全部進程的狀況.get_ident()
查看進程的id.active_count()
查看活躍進程的數量.enumerate()
全部的進程狀況放進一個列表中Lock()
的死鎖現象Lock()
互斥鎖,只有一把鑰匙RLock()
遞歸鎖,只要拿到一把,就等於拿到一串,必須等一串所有歸還下個線程才能繼續拿鑰匙
死鎖狀況多線程
#死鎖狀況 from threading import Thread,Lock from time import sleep #死鎖狀況 from threading import Thread,Lock,RLock from time import sleep mt_lock = Lock() cz_lock = Lock() def miantao(): mt_lock.acquire() print('拿到麪條了') sleep(1) cz_lock.acquire() print('拿到叉子了') print('吃麪') mt_lock.release() cz_lock.release() def chazi(): cz_lock.acquire() print('拿到叉子了') sleep(1) mt_lock.acquire() print('拿到麪條了') print('吃麪') cz_lock.release() mt_lock.release() th1 = Thread(target=miantao, args=()) th2 = Thread(target=chazi, args=()) th1.start() th2.start()
解決死鎖問題RLock()
併發
from threading import Thread, Lock, RLock from time import sleep cz_lock = mt_lock = RLock() #這裏建立遞歸鎖 def miantao(): mt_lock.acquire() print('拿到麪條了') sleep(1) cz_lock.acquire() print('拿到叉子了') print('吃麪') mt_lock.release() cz_lock.release() def chazi(): cz_lock.acquire() print('拿到叉子了') sleep(1) mt_lock.acquire() print('拿到麪條了') print('吃麪') cz_lock.release() mt_lock.release() th1 = Thread(target=miantao, args=()) th2 = Thread(target=chazi, args=()) th1.start() th2.start()
建立就爲阻塞狀態app
from threading import Thread, Event e = Event() def test(): '''檢測數據庫的連通性''' info = input('>>') if info == '1': e.set() print('數據庫網絡鏈接打開') else: print('關閉數據庫網絡鏈接') def connect_q(): '''鏈接數據庫''' print('等待數據庫網絡鏈接') e.wait() print('數據庫鏈接成功!') t1 = Thread(target=test) t2 = Thread(target=connect_q) t1.start() t2.start()
.acquire()
#鑰匙.release()
#釋放鑰匙.notify(num)
#容許鑰匙串有幾把鑰匙.wait()
#等待.notify(num)
提供鑰匙數量ide
#條件 from threading import Condition, Thread def print_thrad(con, i): con.acquire() #這裏也有鎖 con.wait() print('第{}線程運行了'.format(i)) con.release() num = int(input('>>')) con = Condition() for i in range(10): t = Thread(target=print_thrad, args=(con, i)) t.start() con.acquire() #這裏也有鎖 con.notify(num) #先後必須有鑰匙和鎖 con.release()
用法和Threa()
一致ui
from threading import Timer def func(): print('兩秒時間到了') Timer(2, func).start() print('呵呵') #結果 呵呵 兩秒時間到了 #子線程兩秒後纔開啓
.put()
放元素.put_nowait()
#放元素,沒有元素報錯.get()
#取出元素.get_nowait()
#沒有元素報錯