#*************************************************************************** # python的通信模块:1,urllib 2,requests # url 统一资源定位器,也就是文件的路径,网址,是一个字符串 # response = request.urlopen(url)发起请求并得到响应 import urllib from urllib import request url='https://img.zcool.cn/community/01b82f5bac3a7aa801213deadb1922.jpg@1280w_1l_2o_100sh.jpg' def downloader(url): response = request.urlopen(url) # print(response,type(response)) content = response.read() # print(content,type(content)) # <class 'bytes'> file_name = url.split('/')[-1] with open(file_name,'wb') as f: f.write(content) downloader(url) #************下面一个,展示进程的效果**************************************** from multiprocessing import Process import time def dance(): for i in range(3): print('跳第%d段舞'%i) time.sleep(0.5) def sing(): for i in range(4): print('唱第%d首歌'%i) time.sleep(0.5) if __name__ == '__main__': p1 = Process(target=dance) p2 = Process(target=sing) p1.start() p2.start() #***********用进程实现下载器***************************************** from multiprocessing import Process from urllib import request import os def downloader(url,**priority): print(priority['pri'],'进程ID:',os.getpid(),'父进程ID',os.getppid()) file_name = url.split('/')[-1] response = request.urlopen(url) content = response.read() with open(file_name,'wb') as fp: fp.write(content) url_list = ['https://img.zcool.cn/community/01b82f5bac3a7aa801213deadb1922.jpg@1280w_1l_2o_100sh.jpg', 'https://img.zcool.cn/community/01f1d05bac3a7ba801213dea985ecb.jpg@1280w_1l_2o_100sh.jpg', 'https://img.zcool.cn/community/01ccf65bac3a7ea8012099c8d65a89.jpg@1280w_1l_2o_100sh.jpg', 'https://img.zcool.cn/community/01e6435bac3a81a8012099c8ed4c5c.jpg@1280w_1l_2o_100sh.jpg', 'https://img.zcool.cn/community/010f465bac3a8ba801213dea44adaf.jpg@1280w_1l_2o_100sh.jpg', ] p_list = [] name_list = ['进程一:张三','进程二:李四','进程三:王五','进程四:赵六','进程五:刘七'] pri_dit = [{'pri':'1'},{'pri':'0.9'},{'pri':'0.5'},{'pri':'0.1'},{'pri':'0'}] if __name__ == '__main__': i = 0 for url in url_list: p = Process(target=downloader,name=name_list[i],args=(url_list[i],),kwargs=pri_dit[i]) i+=1 p_list.append(p) for p in p_list: p.start() print(p.name,'starts to work') for p in p_list: p.join() print('主进程id',os.getpid()) #***********创建进程的第二种方式,继承Process类***************************************** from multiprocessing import Process import os from urllib import request #步骤1:继承Process类 class Process_Class(Process): # 因为Process类本身也有__init___方法,这个子类相当于重写了这个方法。 # 但这样会带来一个问题,我们并没有完成的初始化一个进程类,所以就不能使用这个类继承的一些方法和属性。 #步骤2:使用父类初始化方法初始子类 def __init__(self,url): Process.__init__(self) self.url = url #步骤3:重写Process类中的run方法 def run(self): print('当前进程id:', os.getpid(), os.getppid()) # 文件名 file_name = self.url.split('/')[-1] # 网络请求 response = request.urlopen(self.url) # 一个响应对象 # 获取响应的内容 content = response.read() # 保存 with open(file_name, 'wb') as fp: fp.write(content) if __name__ == '__main__': url_list = [ 'https://www.baidu.com/img/baidu_jgylogo3.gif', 'http://www.offcn.com/statics/images/zgimg/index2019.png' ] for url in url_list: p = Process_Class(url) p.start() #***********进程池***************************************** from multiprocessing import Pool import time, random def worker(msg): t_start = time.time() print('%s--开始执行' % (msg)) time.sleep(random.random()) # 模拟工作所消耗的时间 t_end = time.time() print('%s--执行完毕,消耗时间为:%0.2f' % (msg, t_end - t_start)) if __name__ == '__main__': p = Pool(3) # 创建一个容量是3的进程池 for i in range(10): # 将进程添加到进程池, # 格式:进程池.apply_async(func=单个进程要调用的目标,args=(参数1,参数2....)) # 每次循环将会用空闲的子进程去调用目标 p.apply_async(func=worker, args=(i,)) # 异步操作 # p.apply(func=worker,args=(i,))#串行操作 print('-----------start----------') # 关闭进程池,关闭后进程池将不再接收新的任务 p.close() # 等待进程池中的所有子进程都结束后,注意:join一定要放在close后面。 p.join() print('------------end----------') #****************************************************
(url)(进程的两种创建方式)(进程池)
最新推荐文章于 2024-05-29 22:56:42 发布