https://www.youtube.com/watch?v=DnTn3Yx-Nvgpython
join功能:git
import threading import time def thread_job2(): print('T2', threading.current_thread()) def thread_job1(): print("-----------T1 begin-----------") for i in range(10): print("job2:", threading.current_thread()) time.sleep(0.1) print("-----------T1 end-----------") def main(): thread1 = threading.Thread(target=thread_job1, name="T1") thread2 = threading.Thread(target=thread_job2, name="T2") thread1.start() thread2.start() thread1.join() # 要等線程所有運行完,才執行下一步。須要加這一句 print(threading.active_count()) print(threading.enumerate()) print(threading.currentThread()) print("all done") if __name__ == '__main__': main()
Queue功能github
https://www.youtube.com/watch?v=DnTn3Yx-Nvg多線程
https://github.com/MorvanZhou/tutorials/blob/master/threadingTUT/thread4_queue.py 代碼 spa
GIL線程
多線程的運算不必定會效率會提高不少,緣由在於 python 的 GIL (global interpreter lock)code
https://www.youtube.com/watch?v=2511-7VR4nQblog
lock鎖進程
https://www.youtube.com/watch?v=-q4txLdUMBMip
https://github.com/MorvanZhou/tutorials/blob/master/threadingTUT/thread6_lock.py
lock和join的區別: lock 是鎖住變量的更新,join 是不讓主線程比某個 thread 先結束
多進程
多核能夠避免上述多線程的劣勢
https://morvanzhou.github.io/tutorials/python-basic/multiprocessing/3-queue/
...