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