import os
size = os.path.getsize('fork.py')
pid = os.fork()
if pid < 0:
print('不想动')
#子进程拷贝前半部分
elif pid == 0:
n = size // 2
fw = open('child','w')
with open('fork.py','r') as f:
while True:
if n < 64:
data = f.read(n)
fw.write(data)
break
data = f.read(64)
#read想把文件一次读完,尽量读取大文件的时候不要使用read
fw.write(data)
n -= 64
fw.close()
#父进程拷贝后半部分
else:
fw = open('parent','w')
with open('fork.py') as f:
f.seek(size//2,0)
while True:
data = f.read(64)
if not data:
break
fw.write(data)
fw.close
python 父子进程分别拷贝文件上下两部分
最新推荐文章于 2020-01-15 10:32:01 发布