你的心如利箭 在风中笔直的飞翔
github DNS ALEXA CDN
jquery JS CSS CSS3 HTML5 svg php --- isux w3cplus

21270

  博客园  :: 首页  ::  ::  ::  :: 管理

https://www.youtube.com/watch?v=DnTn3Yx-Nvg

 

 

 join功能:

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功能

https://www.youtube.com/watch?v=DnTn3Yx-Nvg

https://github.com/MorvanZhou/tutorials/blob/master/threadingTUT/thread4_queue.py  代码 

 

GIL

多线程的运算不一定会效率会提升很多,原因在于 python 的 GIL (global interpreter lock)

 https://www.youtube.com/watch?v=2511-7VR4nQ

 

lock锁

https://www.youtube.com/watch?v=-q4txLdUMBM

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/

 

 

...

posted on 2018-01-28 01:38  bjhhh  阅读(407)  评论(0编辑  收藏  举报