from multiprocessing import Process
from urllib import request
import os
class Process_class(Process):
def __init__(self,url):
self.url=url
Process.__init__(self) #重写父类方法
def run(self):
print('子进程ID', os.getpid(), '\t', '其父进程ID', os.getppid())
file_name = self.url.split('/')[-1]
response = request.urlopen(self.url)
content = response.read()
with open(file_name, 'wb') as f:
f.write(content)
if __name__ == '__main__':
n= [
'https://www.baidu.com/img/bd_logo1.png',
'https://p3.ssl.qhimgs0.com/sdm/180_100_/t01fba1e020fa27fcc1.jpg'
]
for m in n:
p = Process_class(m)
p.start()
创建进程的第二种方法
最新推荐文章于 2023-02-25 16:33:55 发布