"""
创建2个进程,
分别复制文件的上半部分和下半部分到一个新的文件中
"""
import os
from multiprocessing import Process
from time import sleep
filename = './images.jpg'
size = os.path.getsize(filename)
fr = open(filename, 'rb')
def top():
sleep(1)
fw = open('top.jpg', 'wb')
fr.seek(0,0)
n = size // 2
fw.write(fr.read(n))
fr.close()
fw.close()
print('top,完成')
def bot():
fw = open('bot.jpg', 'wb')
n = size // 2
fr.seek(n, 0)
fw.write(fr.read())
fr.close()
fw.close()
print('bot,完成')
if __name__ == '__main__':
p1 = Process(target=top)
p2 = Process(target=bot)
p1.start()
p2.start()
p1.join()
p2.join()
print('face', os.path.getsize(filename))
print('bot', os.path.getsize('bot.jpg'))
print('top', os.path.getsize('top.jpg'))
|