執行緒 守護執行緒 遞迴鎖
阿新 • • 發佈:2018-11-30
------------------------------方法不對,功力全廢;做事講方法,做人講原則.
# # ------------------------------------------------------------------------------------------------------------#
''''
執行緒理論
執行緒是cpu的最小執行單位,程序:資源單位
執行緒的建立
From threading import Thread
T = Thread(target=func,args=())
Class MyThread(Thread):
Def __init__(self,n):
Super().__init()
Self.n = n
Def run(self):
Pass
T = MyThread(12)
執行緒和程序效率對比
Join方法
執行緒之間是資料共享的
同一程序中執行緒之間是資料共享的,全域性變數驗證了一下
鎖(互斥鎖\同步鎖) #Lock,解決資料共享時資料不安全的問題,但是同時了犧牲了效率
死鎖現象:雙方互相等待對方釋放對方手裡拿到的那個鎖
遞迴鎖: Rlock 同樣是互斥的.裡面存著個計數器
守護執行緒:主執行緒等著程序中所有非守護執行緒的結束,才算結束
守護程序:主程序程式碼結束,守護程序跟著結束
訊號量:大寶劍
事件: Event
'''
# # # --------------[執行緒的建立]--------------
#
# import time
# from threading import Thread
# from multiprocessing import Process
# #
# # def func(n):
# # print(n)
# #
# # if __name__ == '__main__':
# # # t = Thread(target=func,args=('我是執行緒',))
# # # t.start() #速度非常快
# # p = Process(target=func,args=('我是程序',))
# # p.start()
# # print('主執行緒結束')
#
# # class MyThread(Thread):
# #
# # def run(self):
# # print('sb明傑')
# #
# # if __name__ == '__main__':
# # t = MyThread()
# # t.start()
# # print('主執行緒結束')
#
#
# # # --------------[同一程序下執行緒是資源共享的]--------------
import time
from threading import Thread
from multiprocessing import Process
num = 100
def func():
global num
num = 0
if __name__ == '__main__':
t = Thread(target=func,)
t.start()
t.join()
print(num)
#
#
# # # --------------[執行緒的和程序的效率對比]--------------
#
# import time
# from threading import Thread
# from multiprocessing import Process
#
# def func(n):
# num = 0
# for n1 in range(n):
# num += n1
# print('num',num)
# if __name__ == '__main__':
# t_s_t = time.time()
# t_list = []
# for i in range(10):
# t = Thread(target=func,args=(10,))
# t.start() #速度非常快
# t_list.append(t)
# [tt.join() for tt in t_list]
# t_e_t = time.time()
# t_dif_t = t_e_t - t_s_t #獲取了執行緒的執行時間
#
# p_s_t = time.time() #開始時間
# p_list = []
# for ii in range(10):
# p = Process(target=func,args=(10,))
# p.start()
# p_list.append(p)
# [pp.join() for pp in p_list]
# p_e_t = time.time() #結束時間
# p_dif_t = p_e_t - p_s_t #時間差
#
# print('執行緒>>>>',t_dif_t)
# print('程序....',p_dif_t)
# print('主執行緒結束')
#
#
## # --------------[驗證執行緒間資料是否共享]--------------
import time
from threading import Thread,Lock
#演示共享資源的時候,資料不安全的問題
# num = 100
# def func():
# global num
# # num -= 1
# mid = num
# mid = mid - 1
# time.sleep(0.0001)
# num = mid
# if __name__ == '__main__':
#
# t_list = []
# for i in range(10):
# t = Thread(target=func,)
# t.start()
# t_list.append(t)
# [tt.join() for tt in t_list]
# print('主執行緒>>>',num)
## # --------------[通過鎖來解決資料不安全]--------------
#的問題,執行緒模組裡面引入的鎖
# num = 100
# def func(t_lock):
# global num
# # num -= 1
# t_lock.acquire()
# mid = num
# mid = mid - 1
# time.sleep(0.0001)
# num = mid
# t_lock.release()
# if __name__ == '__main__':
# t_lock = Lock() #鎖物件(同步鎖,互斥鎖)
# t_list = []
# for i in range(10):
# t = Thread(target=func,args=(t_lock,))
# t.start()
# t_list.append(t)
# [tt.join() for tt in t_list]
#
# print('主執行緒>>>',num)
## --------------[死鎖現象]--------------
import time
from threading import Thread,Lock,RLock
def fuc1(locka,lockb):
locka.acquire()
print("+++++++++++++++a")
lockb.acquire()
print("+++++++++++++++b")
locka.release()
lockb.release()
def fuc2(locka, lockb):
lockb.acquire()
print("+++++++++++++++B")
locka.acquire()
print("+++++++++++++++A")
lockb.release()
locka.release()
if __name__ == '__main__':
# lock_A = Lock()
# lock_B = Lock()
lock_A = lock_B = RLock()
t1 = Thread(target=fuc1, args=(lock_A, lock_B))
t2 = Thread(target=fuc2, args=(lock_A, lock_B))
t1.start()
t2.start()
# # --------------[守護執行緒]--------------
#------主執行緒 會隨著非守護執行緒的結束 而結束
import time
from threading import Thread
from multiprocessing import Process
def func1(n):
time.sleep(5)
print(n)
def func2(n):
time.sleep(3)
print(n)
if __name__ == '__main__':
# # --------------[複習回顧---守護程序]--------------
# ------主執行緒結束 守護程序也結束
#程序+守護程序示例#
p1 = Process(target=func1,args=('子程序1號',))
p1.daemon = True #設定守護,在start之前加
p1.start()
p2 = Process(target=func2, args=('子程序2號',))
# p2.daemon = True
p2.start()
# 執行緒+守護執行緒示例#
# t1 = Thread(target=func1, args=('子執行緒一號',))
# t1.daemon = True #設定守護,在start之前加
# t1.start()
# t2 = Thread(target=func2, args=('子執行緒二號',))
# t2.setdaemon(Ture)
# t2.start().
print('主程序結束!!')
# print('主執行緒結束')