python多執行緒(2) 02 26

2021-09-11 08:29:13 字數 3903 閱讀 3558

join將其他執行緒(主線程)掛起,待當前執行緒結束後,在繼續執行掛起主線程

daemon守護執行緒,主線程結束,守護執行緒被迫停止

import threading,time

def run():

for i in range(100):

time.sleep(0.1)

print("子執行緒結束")

t1 = threading.thread(target=run)

t1.setdaemon(true)#守護執行緒

t1.start()

t1.join(timeout=5)#指定掛起時間

print("主線程結束")#主,子執行緒互不干擾

可重入鎖rlock,又稱遞迴鎖,鎖上加鎖

#哲學家就餐問題

import threading,time

rlock1 = threading.rlock()

rlock2 = threading.rlock()

rlock3 = threading.rlock()

rlock4 = threading.rlock()

rlock5 = threading.rlock()

class zhexuejia():

def __init__(self,left,right):

self.left = left

self.right = right

z1 = zhexuejia(rlock5,rlock1)

z2 = zhexuejia(rlock1,rlock2)

z3 = zhexuejia(rlock2,rlock3)

z4 = zhexuejia(rlock3,rlock4)

z5 = zhexuejia(rlock4,rlock5)

def run(z,name):

f = z.left.acquire()

if f:

print(name,"獲取左筷子")

ff = z.right.acquire()

if ff:

print(name, "獲取右筷子")

print(name,"哲學家開始就餐")

time.sleep(1)

z.right.release()

z.left.release()

t1 = threading.thread(target=run,args=(z1,"z1"))

t2 = threading.thread(target=run,args=(z2,"z2"))

t3 = threading.thread(target=run,args=(z3,"z3"))

t4 = threading.thread(target=run,args=(z4,"z4"))

t5 = threading.thread(target=run,args=(z5,"z5"))

t1.start()

t2.start()

t3.start()

t4.start()

t5.start()

允許限定多個任務同時執行

import threading,time

s = threading.semaphore(3)

#s = threading.lock()

def run(name):

s.acquire()

print(name,"開始執行")

time.sleep(1)

print(name,"執行結束")

s.release()

start_time = time.time()

t1 = threading.thread(target=run,args=("t1",))

t2 = threading.thread(target=run,args=("t2",))

t3 = threading.thread(target=run,args=("t3",))

t4 = threading.thread(target=run,args=("t4",))

t5 = threading.thread(target=run,args=("t5",))

t6 = threading.thread(target=run,args=("t6",))

t7 = threading.thread(target=run,args=("t7",))

t8 = threading.thread(target=run,args=("t8",))

t9 = threading.thread(target=run,args=("t9",))

t10 = threading.thread(target=run,args=("t10",))

t1.start()

t2.start()

t3.start()

t4.start()

t5.start()

t6.start()

t7.start()

t8.start()

t9.start()

t10.start()

t1.join()

t2.join()

t3.join()

t4.join()

t5.join()

t6.join()

t7.join()

t8.join()

t9.join()

t10.join()

end_time = time.time()

print(end_time - start_time)#獲取執行時間

import threading

def deal_task(n):#定時器

print('%s 我被執行了' % n)

t = threading.timer(3,deal_task,args=(10,))

t.start()

import threading,time

e = threading.event()#建立事件

def hld():

print("現在是紅燈")

time.sleep(5)

e.set()

print("現在是綠燈")

def car_run(name):

print(name,"迎面駛來")

e.wait()

print(name,"繼續前行")

for i in range(10):

t = threading.thread(target=car_run,args=(i,))

t.start()

h = threading.thread(target=hld,)

h.start()

#先進後出佇列

import queue

q = queue.lifoqueue()

q.put(1)

q.put('1')

q.put()

print(q.get())

print(q.get())

print(q.get())

執行結果:11

#優先順序佇列

import queue

q = queue.priorityqueue()

q.put((10,'q'))

q.put((30,'z'))

q.put((20,'a'))

print(q.get())

print(q.get())

print(q.get())

執行結果:

(10, 『q』)

(20, 『a』)

(30, 『z』)

python多執行緒 python多執行緒

通常來說,多程序適用於計算密集型任務,多執行緒適用於io密集型任務,如網路爬蟲。關於多執行緒和多程序的區別,請參考這個 下面將使用python標準庫的multiprocessing包來嘗試多執行緒的操作,在python中呼叫多執行緒要使用multiprocessing.dummy,如果是多程序則去掉...

python多執行緒詳解 Python多執行緒詳解

前言 由於最近的工作中一直需要用到python去處理資料,而在面對大量的資料時,python多執行緒的優勢就展現出來了。因而藉此機會,盡可能詳盡地來闡述python多執行緒。但對於其更底層的實現機制,在此不做深究,僅是對於之前的一知半解做個補充,也希望初學者能夠通過這篇文章,即便是照葫蘆畫瓢,也能夠...

python程式多執行緒 PYTHON多執行緒

在單執行緒的情況下,程式是逐條指令順序執行的。同一時間只做乙個任務,完成了乙個任務再進行下乙個任務。比如有5個人吃飯,單執行緒一次只允許乙個人吃,乙個人吃完了另乙個人才能接著吃,假如每個人吃飯都需要1分鐘,5個人就需要5分鐘。多執行緒的情況下,程式就會同時進行多個任務,雖然在同一時刻也只能執行某個任...