Python脚本创建子进程proc = subprocess.Popen()
,它会在〜1秒内写入几行到p.txt
。我想在proc
正在运行时实时处理此脚本中的p.txt
更新。正确避免R/W文件死锁
...
def seek_file(file, process):
while process.poll() is None:
l = file.readline().strip()
if not l:
sleep(0.1)
continue
else:
yield tuple(l.split('='))
...
p_file = open('p.txt')
data_tuples = seek_file(p_file, proc)
for d in data_tuples:
print(d)
...
为什么这段代码偶尔会变成死锁状态?处理经常更新的文件并显示进度的最佳方法是什么?
我假设'yeild'是一个错字? – ShadowRanger