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

    import  threading
    import time

    ###############################多线程###############################
    #----------函数无参数,子线程等待方式 循环join
    def run():
    time.sleep(2)
    print("正在执行" ,threading.current_thread())
    starttime=time.time()
    threads=[]
    for i in range(3):
    t=threading.Thread(target=run)
    threads.append(t)
    t.start()
    for t in threads:
    t.join()
    endtime=time.time()
    print(endtime-starttime)


    #----------函数无参数,子线程等待方式 循环join函数带参数run(name) 线程传递参数args=('ABC',) 一个参数元组必须用“,”,子线程等待方式 while threading.active_count() != 1
    def run(name):
    time.sleep(2)
    print("正在执行:%s" %name,threading.current_thread())
    starttime=time.time()

    for i in range(3):
    t=threading.Thread(target=run,args=('ABC',))
    t.start()
    while threading.active_count() !=1:
    print("子线程已执行完毕")
    endtime = time.time()
    print(endtime - starttime)

    ################################线程池###############################
    import threadpool
    #辅助方法
    import pymongo,requests
    client = pymongo.MongoClient(host='118.24.3.40',port=27017)
    table = client['likun']['qq_group_likun']
    all_qq = [i.get('qq') for i in table.find()]
    url = 'http://q4.qlogo.cn/g?b=qq&nk=%s&s=140'

    def down_img(qq_num):
    res = requests.get(url%qq_num).content
    with open('%s.jpg'%qq_num,'wb') as fw:
    fw.write(res)

    #线程池实现
    pool = threadpool.ThreadPool(200) #线程池的大小
    all_requests = threadpool.makeRequests(down_img,all_qq)#分配数据 makeRequests(函数,list)
    for r in all_requests:
    pool.putRequest(r) #发请求
    pool.wait()#等待所有线程运行完
    print('done!下载完成。')



    #################################进程池###############################
    from multiprocessing import Process,Pool,active_children
    import pymongo,requests
    import threading
    #辅助方法
    client = pymongo.MongoClient(host='118.24.3.40',port=27017)
    table = client['likun']['qq_group_likun']
    all_qq = [i.get('qq') for i in table.find()]
    url = 'http://q4.qlogo.cn/g?b=qq&nk=%s&s=140'
    def down_img(qq_num):

    res = requests.get(url%qq_num).content
    with open('%s.jpg'%qq_num,'wb') as fw:
    fw.write(res)
    #进程池实现
    if __name__ == '__main__':
    # for qq in all_qq:
    # p = Process(target=down_img,args=(qq,)) #启动多进程
    # p.start()
    pool = Pool(5)#指定进程池的大小
    list(pool.map(down_img,all_qq)) #运行 使用进程池
  • 相关阅读:
    使用bash编写Linux shell脚本参数和子壳
    开发项目的简单流程(需求、数据库、编码)
    hadoop和Hive的数据处理流程
    数据分析
    模糊聚类分析的实现
    贝叶斯1
    代理猎手
    贝叶斯2
    模糊聚类算法(FCM)和硬聚类算法(HCM)的VB6.0实现及
    C++模板
  • 原文地址:https://www.cnblogs.com/wenchengqingfeng/p/9453711.html
Copyright © 2011-2022 走看看