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

    ...

  • 相关阅读:
    赛孚耐(SafeNet)加密狗 C#调用代码
    转 RMAN-20033
    MyBatis <foreach>
    MySQL InnoDB锁问题
    MySQL MyISAM表锁
    MySQL锁概述
    MySQL 优化表数据类型
    MySQL 优化分页思路
    MySQL EXPLAIN
    MySQL 开启慢查询日志
  • 原文地址:https://www.cnblogs.com/qq21270/p/8368458.html
Copyright © 2020-2023  润新知