zoukankan      html  css  js  c++  java
  • py库:threading

    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/

    ...

  • 相关阅读:
    100 numpy exercises
    IndentationError: unindent does not match any outer indentation level
    Git详解之七:自定义Git
    Git详解之六:Git工具
    Git详解之五:分布式Git
    Git详解之四:服务器上的Git
    Git详解之三:Git分支
    Git详解之二:Git基础
    Git详解之一:Git起步
    Jquery基础之事件操作
  • 原文地址:https://www.cnblogs.com/qq21270/p/8368458.html
Copyright © 2011-2022 走看看