from urllib import request
from multiprocessing import Process
import os
def downloader(url,**kwargs):
print('当前进程id:',os.getpid(),os.getppid())
print('关键字参数:',kwargs['pro'])
#文件名
file_name = url.split('/')[-1]
#网络请求
response = request.urlopen(url)#一个响应对象
#获取响应的内容
content = response.read()
#保存
with open(file_name,'wb') as fp:
fp.write(content)
if __name__ == '__main__':
# for i in range(1,5,1):
# url = 'http://www.langlang2017.com/img/banner'+str(i)+'.png'
# print(url)
# p = Process(target=downloader,args=(url,))
# p.start()
url_list = [
'https://www.baidu.com/img/baidu_jgylogo3.gif',
'http://www.offcn.com/statics/images/zgimg/index2019.png'
]
name_list = ['进程1--张翠花','进程2---赵铁蛋']
p_list = []#进程列表
i = 0
for url in url_list:
n = name_list[i]
i = i+1
p = Process(target=downloader,name=n,args=(url,),kwargs={'pro':"最高等级"})
print('子进程(%s)将要被运行~!'%p.name)
p.start()
p_list.append(p)
for p in p_list:
p.join()
print('主进程id:',os.getpid())