• Cpython全局解释器锁原理剖析


    """
    In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple
    native threads from executing Python bytecodes at once. This lock is necessary mainly
    because CPython’s memory management is not thread-safe. (However, since the GIL
    exists, other features have grown to depend on the guarantees that it enforces.)
    """
    """
    GIL是一个互斥锁:保证数据的安全(以牺牲效率来换取数据的安全)
    阻止同一个进程内多个线程同时执行(不能并行但是能够实现并发)
    并发:看起来像同时进行的
    GIL全局解释器存在的原因是因为Cpython解释器的内存管理不是线程安全的
    例如:
    线程一正在产生一个变量还没有绑定,这时候垃圾回收线程把它回收掉了,
    对线程一来说就产生了线程数据错误

    垃圾回收机制
    1.引用计数
    2.标记清除
    3.分代回收

    同一个进程下的多个线程不能实现并行但是能够实现并发,多个进程下的线程能够实现并行

    问题:python多线程是不是就没有用了呢?
    四个任务:计算密集的任务 每个任务耗时10s
    单核情况下:
    多线程好一点,消耗的资源少一点
    多核情况下:
    开四个进程:10s多一点
    开四个线程:40s多一点

    四个任务:IO密集的任务 每个任务io 10s
    单核情况下:
    多线程好一点
    多核情况下:
    多线程好一点
    多线程和多进程都有自己的优点,要根据项目需求合理选择

    """

    # 计算密集型
    # from multiprocessing import Process
    # from threading import Thread
    # import os,time
    # def work():
    # res=0
    # for i in range(100000000):
    # res*=i
    #
    #
    # if __name__ == '__main__':
    # l=[]
    # print(os.cpu_count()) # 本机为8核
    # start=time.time()
    # for i in range(8):
    # # p=Process(target=work) #耗时9.252728939056396
    # p=Thread(target=work) #耗时35.369622230529785
    # l.append(p)
    # p.start()
    # for p in l:
    # p.join()
    # stop=time.time()
    # print('run time is %s' %(stop-start))


    # IO密集型
    from multiprocessing import Process
    from threading import Thread
    import threading
    import os,time
    def work():
    time.sleep(2)


    if __name__ == '__main__':
    l=[]
    print(os.cpu_count()) #本机为8核
    start=time.time()
    for i in range(600):
    p=Process(target=work) #耗时4.699530839920044
    # p=Thread(target=work) #耗时2.054128885269165
    l.append(p)
    p.start()
    for p in l:
    p.join()
    stop=time.time()
    print('run time is %s' %(stop-start))
  • 相关阅读:
    canvas性能优化——离屏渲染
    event.target 和 event.currentTarget 的区别
    Electron 主进程和渲染进程互相通信
    谈谈 JS 垃圾回收机制
    【Vue】Vue中render函数用到的with(this)中with的用法及其优缺点
    Java递归读取文件路径下所有文件名称并保存为Txt文档
    Java读取Excel指定列的数据详细教程和注意事项
    Sybase ASE无响应的又一个情况
    AWR报告导出的过程报ORA-06550异常
    如何借助浏览器Console使用Js进行定位和操作元素
  • 原文地址:https://www.cnblogs.com/dongxixi/p/10836218.html
Copyright © 2020-2023  润新知