zoukankan      html  css  js  c++  java
  • python--多线程&多进程

    进程:

             进程就是一组资源的集合

             线程是在进程里面干活的

            一个进程里面至少有一个线程,这个线程就是主线程

    电脑CPU是几核的就只能同时运行几个任务(线程)

    python里面的多线程其实只能用一个核

    GIL  全局解释器锁

    锁:多线程同时操作同一数据时必须加锁

    CPU密集型任务 (CPU干活)   -----多进程

    IO密集型任务(磁盘IO、网络IO)-----多线程

    启动多线程、主线程等待子线程结束

    #进程就是一组资源的集合
    # 线程是在进程里面具体干活的
    
    import threading
    import time
    
    def run():
        time.sleep(5)
        print('over')
    
    start_time = time.time()
    for i in range(20):  #需要多少个线程循环多少次
        t = threading.Thread(target=run)  #实例化一个线程
        t.start()  #启动这个线程
    
    print('运行的时候有几个活跃线程',threading.activeCount())  #21   threading.activeCount() 当前有多少个线程在活跃
    
    # while循环--主线程等待子线程运行结束
    while threading.activeCount() !=1:  #当所有的子进程都结束时,只剩一个主线程
        pass
    
    print('现在几个活跃线程',threading.activeCount())   #1
    ent_time = time.time()
    print(ent_time-start_time)
    
    
    # for循环--主线程等待子线程运行结束
    '''
    start_time = time.time()
    ths = []
    for i in range(20):  #需要多少个线程循环多少次
        t = threading.Thread(target=run)  #实例化一个线程
        t.start()  #启动这个线程
        # t.join()  #在这里加join,线程就变成串行的了
        ths.append(t)  #建一个list,把每个已启动的线程存起来
    
    for thread in ths:
        thread.join()   #循环等待每个线程结束
    
    ent_time = time.time()
    print(ent_time-start_time)  #5.004632949829102
    '''

    继承Thread类的方法启动多线程

    import threading
    import time
    
    class MyThread(threading.Thread):  #继承Tread类
        def run(self): #这个方法必须叫run  重写父类run方法
            time.sleep(5)
            print('run')
    
    for i in range(5):
        t = MyThread()   #实例化一个线程
        t.start()    #start()方法启动线程

    获取多线程的返回值

    import requests
    import threading
    
    all_res = []  #定义一个list,保存所有的返回结果
    def get_name(name):
        r = requests.get('http://api.nnzhp.cn/api/user/stu_info?stu_name=%E5%B0%8F%E9%BB%91',
                         params=name)
        res = r.json()
        all_res.append(res)  #将这次请求的返回结果保存到 all_res中
    
    for i in range(10):
        t = threading.Thread(target=get_name,args=(i,))
        t.start()
    
    while threading.activeCount()!= 1:
        pass
    
    print(all_res)

    守护线程

    #主线程 : 秦始皇
    #守护线程:秦始皇修护陵墓的人
    
    import threading
    import time
    
    def hhh():
        time.sleep(5)
        print('hhh')
    
    for i in range(100):
        t = threading.Thread(target=hhh)
        t.setDaemon(True)   #设置子线程为守护线程  主线程死的时候,守护线程也得死
        t.start()
    
    print('秦始皇死了')

    import threading
    
    num = 0   #全局变量
    lock =threading.Lock() #申请一把锁 多线程同时操作同一个数据的时候一定要加锁
    
    def xiaojun():
        global num   #想要修改全局变量,先声明它是global类型
        # lock.acquire() #加锁
        # num += 1
        # lock.release() #解锁   只加锁不解锁会导致死锁
        with lock: #with自动管理上下文 不需自己手动加锁/解锁
            num+=1
    
    for i in range(100):
        t = threading.Thread(target=xiaojun)
        t.start()
    
    while threading.activeCount()!=1:
        pass
    
    print(num)

    多进程

    from multiprocessing import Process
    import time
    import threading
    
    '''启动10个进程,每个进程启动10个线程'''

    def run_thread(): time.sleep(60) print('%s在运行'%threading.current_thread()) #threading.current_thread() 当前线程名 def run(): for i in range(10): t = threading.Thread(target=run_thread) t.start() if __name__ =='__main__': for i in range(10): p = Process(target=run) #实例化一个进程 p.start() #启动进程 print(p.pid) #打印进程号
  • 相关阅读:
    vue组件基本结构及各个生命周期
    vue项目目录结构详解
    http请求报文格式和响应报文格式
    H5中对history栈的操作
    原生js实现元素类名的判存、添加和移除
    Content-type解析
    IntelliJ Idea 常用快捷键列表
    深入ES6中的class类
    v-model和 .sync
    作用域插槽
  • 原文地址:https://www.cnblogs.com/HathawayLee/p/9978260.html
Copyright © 2011-2022 走看看