????? ?????
??? ???? ??? ? ???? ?? ??? ????? ????. ??? ?? ??? ????.
?? ???
?? ?? ??. ??? ???? ?
?? ?
???????. Wait Locked Sleeping
Dead
????? ??? ??? ? ????. ????? ??? ??? ??? ????. ????? ?? ? ????:
?? ???
?? ???
?? ???(????? ???)
????? ???
??? ???? ??? ? ???? ?? ???? ???? ?????.
1. ??? ??
Python? ?? ??? ??? ?? ? ?? ??, ? ???? ???? ?????.
??? ????? ?? ??? ??? ?? ?? ??? ???? ????? ???? ????. ?????? ?? ??? ????? ?????.
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import time import threading class MyThread(threading.Thread): def run(self): for i in range(5): print('thread {}, @number: {}'.format(self.name, i)) time.sleep(1) def main(): print("Start main threading") # 創(chuàng)建三個(gè)線程 threads = [MyThread() for i in range(3)] # 啟動(dòng)三個(gè)線程 for t in threads: t.start() print("End Main threading") if __name__ == '__main__': main()
?? ??:
Start main threading thread Thread-1, @number: 0 thread Thread-2, @number: 0 thread Thread-3, @number: 0 End Main threading thread Thread-2, @number: 1 thread Thread-1, @number: 1 thread Thread-3, @number: 1 thread Thread-1, @number: 2 thread Thread-3, @number: 2 thread Thread-2, @number: 2 thread Thread-2, @number: 3 thread Thread-3, @number: 3 thread Thread-1, @number: 3 thread Thread-3, @number: 4 thread Thread-2, @number: 4 thread Thread-1, @number: 4
??? ?? ?? ??? ?? ??? ??? ????.
2. ??? ??(?? ??)
?? ??? ??? ???? ?? ???? ?? ??? ?? ???? ?? ?? ????. ??? ???? ?? ?? ???? ??? ??? ??? ???? ?? ???? ?????.
?? Join ??? ????? ???.
? ???? ??? ?? ??? ?????.
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import time import threading class MyThread(threading.Thread): def run(self): for i in range(5): print('thread {}, @number: {}'.format(self.name, i)) time.sleep(1) def main(): print("Start main threading") # 創(chuàng)建三個(gè)線程 threads = [MyThread() for i in range(3)] # 啟動(dòng)三個(gè)線程 for t in threads: t.start() # 一次讓新創(chuàng)建的線程執(zhí)行 join for t in threads: t.join() print("End Main threading") if __name__ == '__main__': main()
??? ??? ?? ? ??? ??? ??? ???? ?? ???? ?? ???? ??? ??? ??? ? ????? ?? ??? ? ? ????. ???? ???.
Start main threading thread Thread-1, @number: 0 thread Thread-2, @number: 0 thread Thread-3, @number: 0 thread Thread-1, @number: 1 thread Thread-3, @number: 1 thread Thread-2, @number: 1 thread Thread-2, @number: 2 thread Thread-1, @number: 2 thread Thread-3, @number: 2 thread Thread-2, @number: 3 thread Thread-1, @number: 3 thread Thread-3, @number: 3 thread Thread-3, @number: 4 thread Thread-2, @number: 4 thread Thread-1, @number: 4 End Main threading
3. ??? ??? ? ??? ??
??? ??? ?????? ???? ??? ????? ???? ????? ????. ?? ?? ???? ?? ? ????. ?, ???? ????? ???? ???? ????? ?? ??? ???? ???.
??? ??? ?? ??? ?????.
lock = threading.Lock()
????? ?? ??
lock.acquire()
?? ??? ??? ??? ???? ???
lock.release()
??, ??? ????? ??? ???? ?? ?? ??? ???? ?? Python? ??? ??(RLock)? ?????. ). RLock? ????? Lock? ??? ??? ????, ???? ?? ??? ????? ???? ?? ? ??? ? ????. ???? ?? ??? ??? ??? ?? ???? ???? ?? ? ????.
?? ??? ??? ??? ??? ?????? ??? ????? ???.
r_lock = threading.RLock()
4. ?? ?? ??
???? ??? ??? ???? ??? ? ??? ? ??? ????? ??? ?? ?? ??? ??? ??? ???. Python? Condition ??? ?????. Condition ??? ?? ???? ?????? ?? ??? ??? ? ???? ???? ? ??? ? ??? Lock ??? ?? ?? ? ?? ?? ??? ?? ? ?? ??? ?????. ???? ?? ?? ?? ??? ?????. ??? ????? ???? ???? ??? ???? ???? ???? ?? ????? ?? ? ????. ?? ??? ?? ???? ??? ?? ? ??? ?? ?????.
?? ??? ?? ?? ???? ??? ??? ???? ??? ? ? ????. ??? ???? ??? ?? ?(Lock ?? RLock)? ???? ??? ??? ? ????. ?? ???? ?? ??? ? ??? ?? ?????. ? ????? ??? ??? ??? ???? ?? ????? ?????.
? ??? ???-??? ???? ?? ?????. ?? ??? ?? ??? ? ???? ?? ?????.
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import threading, time class Consumer(threading.Thread): def __init__(self, cond, name): # 初始化 super(Consumer, self).__init__() self.cond = cond self.name = name def run(self): # 確保先運(yùn)行Seeker中的方法 time.sleep(1) self.cond.acquire() print(self.name + ': 我這兩件商品一起買(mǎi),可以便宜點(diǎn)嗎') self.cond.notify() self.cond.wait() print(self.name + ': 我已經(jīng)提交訂單了,你修改下價(jià)格') self.cond.notify() self.cond.wait() print(self.name + ': 收到,我支付成功了') self.cond.notify() self.cond.release() print(self.name + ': 等待收貨') class Producer(threading.Thread): def __init__(self, cond, name): super(Producer, self).__init__() self.cond = cond self.name = name def run(self): self.cond.acquire() # 釋放對(duì)瑣的占用,同時(shí)線程掛起在這里,直到被 notify 并重新占有瑣。 self.cond.wait() print(self.name + ': 可以的,你提交訂單吧') self.cond.notify() self.cond.wait() print(self.name + ': 好了,已經(jīng)修改了') self.cond.notify() self.cond.wait() print(self.name + ': 嗯,收款成功,馬上給你發(fā)貨') self.cond.release() print(self.name + ': 發(fā)貨商品') cond = threading.Condition() consumer = Consumer(cond, '買(mǎi)家(兩點(diǎn)水)') producer = Producer(cond, '賣(mài)家(三點(diǎn)水)') consumer.start() producer.start()
?? ??? ??? ????.
買(mǎi)家(兩點(diǎn)水): 我這兩件商品一起買(mǎi),可以便宜點(diǎn)嗎 賣(mài)家(三點(diǎn)水): 可以的,你提交訂單吧 買(mǎi)家(兩點(diǎn)水): 我已經(jīng)提交訂單了,你修改下價(jià)格 賣(mài)家(三點(diǎn)水): 好了,已經(jīng)修改了 買(mǎi)家(兩點(diǎn)水): 收到,我支付成功了 買(mǎi)家(兩點(diǎn)水): 等待收貨 賣(mài)家(三點(diǎn)水): 嗯,收款成功,馬上給你發(fā)貨 賣(mài)家(三點(diǎn)水): 發(fā)貨商品
5. communications
????? ?? ?? ???? ??, ? ????? ????? ?? ???? ???. ???? ??? ??? ?? ??? ???? ???? ????? ??? ?? ????
??? ? ????? ?? ???? ???? ??? ?? ??? ??? ??? ?????? ???? ???? ????. put() ? get() ??? ???? ????? ??? ????? ???? ?? ???? ???? Queue ??? ????.
# -*- coding: UTF-8 -*- from queue import Queue from threading import Thread isRead = True def write(q): # 寫(xiě)數(shù)據(jù)進(jìn)程 for value in ['兩點(diǎn)水', '三點(diǎn)水', '四點(diǎn)水']: print('寫(xiě)進(jìn) Queue 的值為:{0}'.format(value)) q.put(value) def read(q): # 讀取數(shù)據(jù)進(jìn)程 while isRead: value = q.get(True) print('從 Queue 讀取的值為:{0}'.format(value)) if __name__ == '__main__': q = Queue() t1 = Thread(target=write, args=(q,)) t2 = Thread(target=read, args=(q,)) t1.start() t2.start()
?? ??? ??? ????.
寫(xiě)進(jìn) Queue 的值為:兩點(diǎn)水 寫(xiě)進(jìn) Queue 的值為:三點(diǎn)水 從 Queue 讀取的值為:兩點(diǎn)水 寫(xiě)進(jìn) Queue 的值為:四點(diǎn)水 從 Queue 讀取的值為:三點(diǎn)水 從 Queue 讀取的值為:四點(diǎn)水
Python? ??? ? ??? ?? ??? ??? ?????. ?? ????? ??? ?? ??????. ?? ???? true? ?? ?? ???? ??? ??? ??? ?????. .
??? ??? ??? ?? ??? ???? ?? ?? ??, ?? ???, ?? ?? ???? ??? ??? ?? ????? ?????.
?? ??
???? set() ???? ???? ??? ?? ??? ?? ???? true? ?????. Event ??? ?? ?? ???? ??? ???? ?? isSe() ???? ?????. ??? ??? set() ???? ??? ? isSet() ???? true? ?????
Clear the signal
Event ???clear() ???? ???? Event ?? ??? ?? ???? ?? ? ????. ?, Event? ???? ?? Clear ??? ?? isSet() ???? false? ?????.
Waiting
Event ??? wait ???? ?? ??? true? ???? ??? ???? ??? ?????. Event ??? ?? ?? ???? false? ?? wait ???? ???? ?? true? ? ??? ?????.
?:
# -*- coding: UTF-8 -*- import threading class mThread(threading.Thread): def __init__(self, threadname): threading.Thread.__init__(self, name=threadname) def run(self): # 使用全局Event對(duì)象 global event # 判斷Event對(duì)象內(nèi)部信號(hào)標(biāo)志 if event.isSet(): event.clear() event.wait() print(self.getName()) else: print(self.getName()) # 設(shè)置Event對(duì)象內(nèi)部信號(hào)標(biāo)志 event.set() # 生成Event對(duì)象 event = threading.Event() # 設(shè)置Event對(duì)象內(nèi)部信號(hào)標(biāo)志 event.set() t1 = [] for i in range(10): t = mThread(str(i)) # 生成線程列表 t1.append(t) for i in t1: # 運(yùn)行線程 i.start()
?? ??? ??? ????.
1 0 3 2 5 4 7 6 9 8
6. ????? ???
????? ?? ???? ???? ???? ?? ???? ??? ????. ?? ?? ?? ???? ??? ??? ?? ???? ?? ?????. ?? ???? ???? ?? ?? ???? ???? ? ?? ???? ??? ??? ?? ???? ????? ???? ???? ???. Python? setDeamon ???? ?????.