Python中如何使用线程池和进程池

线程池

import threadpool, time
 
with open(r'../uoko_house_id.txt', 'r', encoding='utf-8') as f:    # with open语句表示通用的打开文件的方式,此处用来获取需要爬取参数的列表
    roomIdLi = f.readlines()
    roomIdList =[x.replace('\n','').replace(' ','') for x in roomIdLi]
    print(roomIdList)
    li = [[i, item] for i, item in enumerate(roomIdList)]    # enumerate()将列表中元素和其下标重新组合输出
 
def run(roomId):
    """对传入参数进行处理"""
    print('传入参数为:', roomId)
    time.sleep(1)
  
def main():
    roomList = li       # 房间信息
    start_time = time.time()
    print('启动时间为:', start_time)
    pool = threadpool.ThreadPool(10)
    requests = threadpool.makeRequests(run, roomList)
    [pool.putRequest(req) for req in requests]
    pool.wait()
    print("共用时:", time.time()-start_time)
 
if __name__ == '__main__':
    main()

进程池

from multiprocessing.pool import Pool
from time import sleep
 
def fun(a):
    sleep(5)
    print(a)
 
if __name__ == '__main__':
    p = Pool()             
    for i in range(10):
        p.apply_async(fun, args= (i, ))
    p.close()
    p.join()       
    print("end")

你可能感兴趣的:(Python中如何使用线程池和进程池)