Re: [閒聊] python有些很奇怪的地方...

看板Python作者 (huggie)時間18年前 (2006/01/08 23:00), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串5/6 (看更多)
※ 引述《Leland (報廢機器人的傳說)》之銘言: : ※ 引述《huggie (huggie)》之銘言: : : 我自己會用 : : for line in open('/etc/xxx', 'r'): : : print line : : 這樣應該也不用 close() 了 : : 因為應該會 out of scope 然後自動被 garbage collect 吧! : : 我是這麼想的 : 我不確定這樣 out of scope 會不會被 garbage collect 耶, : 就算會我也不知道會不會自己 close 掉。 我剛剛在 Linux 上面做了一點小測試 fd = [] while 1: fd.append(open('data', 'r')) 幾乎在我按下 enter 執行的同時就會發現已經超過系統負荷了: usa:/home/thw/diginorth/dbEST# ./test.py Traceback (most recent call last): File "./test.py", line 7, in ? IOError: [Errno 24] Too many open files: 'data' 但如果是... while 1: fd = open('data', 'r') 或者是 while 1: for line in open('data', 'r'): pass 就會跑不完,因此我猜 garbage collection 把 fd 清除後會關檔 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.129.65.137

01/08 23:43, , 1F
酷...我沒想過有這種測試方法:Q
01/08 23:43, 1F
文章代碼(AID): #13mIaMLC (Python)
討論串 (同標題文章)
文章代碼(AID): #13mIaMLC (Python)