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/
...