26 python 初學(線程、同步鎖、死鎖和遞歸鎖)
阿新 • • 發佈:2019-01-30
線程 容易 daemon python color col 當前 emp oba
參考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html
並發:一段時間內做一些事情
並行:同時做多件事情
線程是操作系統能夠進行運算調度的基本單位,一個線程就是一個指令集
IO 密集型任務或函數 計算密集型任務函數
t1 = threading.Thread( target=foo, args=( , ))
t1.start()
# _author: lily # _date: 2019/1/29 import threading import time class MyThread(threading.Thread): def __init__View Code(self, num): threading.Thread.__init__(self) self.num = num def run(self): # 定義每個線程要運行的函數 print(‘running on number %s‘ % self.num) time.sleep(3) if __name__ == ‘__main__‘: t1 = MyThread(1) t2 = MyThread(2) t1.start() t2.start()
# _author: lilyView Code# _date: 2019/1/28 import threading import time def music(func): for i in range(2): print(‘listening to music %s.%s‘ % (func, time.ctime())) time.sleep(1) print(‘end listening %s‘ % time.ctime()) def move(func): for i in range(2): print(‘watching at the %s.%s‘ % (func, time.ctime())) time.sleep(5) print(‘end watching %s‘ % time.ctime()) threads = [] t1 = threading.Thread(target=music, args=(‘七裏香‘, )) threads.append(t1) t2 = threading.Thread(target=move, args=(‘阿甘正傳‘, )) threads.append(t2) if __name__ == ‘__main__‘: for t in threads: t.start() print(‘all over %s‘ % time.ctime())
GIL: 全局解釋器鎖。 對於一個進程,在同一時刻,python解釋器中只允許一個線程運行。
結論:在 python裏,如果是 io 密集型,可以用多線程
計算密集型,改 C。
守護線程: t.setDaemon(True) 當主線程結束之後就認為程序執行完畢,不會等待 t 線程執行完畢。
得到當前線程: print(threading.current_thread())
得到當前活著的線程: print(threading.active_count())
同步鎖:
原因:1. 線程共享同一資源,且進行 IO 阻塞時,對資源的操作容易被覆蓋
- 使用 join 就會造成船串行,失去了多線程的意義
使用:r = threading.Lock()
同步鎖與GIL關系:
沒有GIL ,使用同步鎖,可以達到一樣得效果。
# _author: lily # _date: 2019/1/29 import time import threading num = 100 def add(): global num # num -= 1 r.acquire() temp = num # time.sleep(0.0000001) print(‘ok‘) num = temp - 1 r.release() thread_list = [] r = threading.Lock() for i in range(100): t = threading.Thread(target=add) t.start() thread_list.append(t) for thd in thread_list: thd.join() print(‘final num: ‘, num)View Code
線程死鎖和遞歸鎖:
lock = threading.Lock()
lock = threading.RLock()
# _author: lily # _date: 2019/1/29 import threading import time class MyThread(threading.Thread): def __init__(self, name): threading.Thread.__init__(self) self.name = name def run(self): self.do_a() self.do_b() def do_a(self): # lock_a.acquire() my_lock.acquire() print(‘do_a: thread %s get lock A‘ % self.name) time.sleep(3) my_lock.acquire() print(‘do_a: thread %s get lock B‘ % self.name) # lock_b.release() # lock_a.release() my_lock.release() my_lock.release() def do_b(self): # lock_b.acquire() my_lock.acquire() print(‘do_b: thread %s get lock B‘ % self.name) time.sleep(2) # lock_a.acquire() my_lock.acquire() print(‘do_b: thread %s get lock A‘ % self.name) # lock_a.release() # lock_b.release() my_lock.release() my_lock.release() # lock_a = threading.Lock() # lock_b = threading.Lock() my_lock = threading.RLock() thread_list = [] for i in range(5): t = MyThread(i) thread_list.append(t) t.start() for t in thread_list: t.join()View Code
26 python 初學(線程、同步鎖、死鎖和遞歸鎖)