• 进程和线程


    线程池

    import threadpool
    import threading
    import requests
    import hashlib
    
    def down_load_file(url):
        r = requests.get(url)
        m = hashlib.md5(url.encode())
        print('正在下载====%s'%m.hexdigest())
        with open('%s.jpg'%m.hexdigest(),'wb') as fw:
            fw.write(r.content)
    
    
    url_list = ['http://www.nnzhp.cn/wp-content/uploads/2019/02/jiami.jpeg',
                'http://www.nnzhp.cn/wp-content/uploads/2019/03/js.png',
                'http://www.nnzhp.cn/wp-content/uploads/2018/08/ab389f04cb5b57344ef9655428bccaec.png'
                ]
    pool = threadpool.ThreadPool(10)#创建一个线程池,指定线程池最多有多少个线程
    
    reqs = threadpool.makeRequests(down_load_file,url_list) #调用生成启动线程用的参数
    
    # for req in reqs:
    #     pool.putRequest(req)
    
    [pool.putRequest(req) for req in reqs]
    
    print('当前的线程数',threading.activeCount())
    
    pool.wait() #等待
    
    print('测试结束')

    守护线程

    import time,threading,os
    import threadpool
    
    count = 0
    lock = threading.Lock() #申请一把锁
    
    def lajfenlei():
        global count
    
        # lock.acquire()
        # count += 1
        # lock.release()
        with lock:
            count+=1
        print('干垃圾')
    
    for i in range(10):
        syy = threading.Thread(target=lajfenlei,)
        # syy.setDaemon(True) #把子线程设置成守护线程
        syy.start()
    while threading.activeCount() != 1:
        pass
    
    print(count)
    print('完成!')
    
    
    #8
    #python GIL 全局解释器锁,导致python的多线程利用不了多核cpu,但是多进程可以。
    
    #

    多进程

    import multiprocessing
    import time
    import threading
    
    def say():
        time.sleep(2)
        print('hhhh')
    
    def lajfenlei():
        for i in range(10):
            t = threading.Thread(target=say)
            t.start()
        print(threading.activeCount())
        print('垃圾分类')
    
    if __name__ == '__main__':
        for i in range(5):
            p = multiprocessing.Process(target=lajfenlei)
            p.start()
            print(p.pid )
    
        while len(multiprocessing.active_children())!=0:#等待子进程执行完成
            pass
    
        print('子进程都运行完了。')

    多线程

    import threading
    import requests,time
    
    def lajfenlei():
        time.sleep(2)
        print('干垃圾')
    
    # start_time = time.time()
    # for i in range(10):
    #     lajfenlei()
    # print( time.time() - start_time)
    
    def shangke():
        print('上课')
    
    start_time = time.time()
    
    
    #第一种方法,主线程等待子线程
    # threads =[]
    #
    # for i in range(10):
    #     syy = threading.Thread(target=lajfenlei,)
    #     syy.start()
    #     threads.append(syy)
    #
    # for t in threads:
    #     t.join()
    
    for i in range(10):
        syy = threading.Thread(target=lajfenlei,)
        syy.start()
    
    while True:
        if threading.activeCount()==1:
            break
    
    end_time = time.time()
    
    print(end_time - start_time)
  • 相关阅读:
    [转]在WEB下的客户端控件的开发应用
    [转]C#将文件保存到数据库中或者从数据库中读取文件
    [转]如何组织一个高效的开发团队
    [转]使用C#调用金诚信71x系列读卡器的DLL
    [转]统一建模语言UML轻松入门之综合实例
    [转]Web application 的压力测试 MS Web Application Stress Tool (was)
    [转]使用NUnit在.Net编程中进行单元测试
    国外著名人脸识别介绍文章
    js与as通信
    php访问mysql 封装
  • 原文地址:https://www.cnblogs.com/Dorami/p/11359447.html
Copyright © 2020-2023  润新知