파이썬의 스레드와 tempfile 모듈에 흥미로운 문제가 있습니다. 스레드가 종료 될 때까지 뭔가 지워지지 않는 것처럼 보입니다. 열려있는 파일 제한에 대해 실행 중입니다. (이것은 OS X 10.5.8, Python 2.5.1입니다.)파이썬 임시 파일 모듈과 스레드가 훌륭하게 재생되지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
그러나 tempfile 모듈이 수행하는 작업 (모든 보안 검사가 아니라 파일 설명자를 생성 한 후 OS를 사용)을 복제하면됩니다. fdopen을 사용하여 파일 객체 생성) 아무런 문제가 없습니다.
파이썬에 버그로 제출하기 전에 여기에서 확인해 보았습니다. 뭔가 잘못 처리 한 것 같아서요. 그러나 만약 내가 그렇다면, 그걸 알아 내려고 노력하는 날이 어디에도 없었습니다. 나는 worker_notempfile
라인 활성과 worker_tempfile
라인을 주석 처리로 실행하면
#!/usr/bin/python
import threading
import thread
import tempfile
import os
import time
import sys
NUM_THREADS = 10000
def worker_tempfile():
tempfd, tempfn = tempfile.mkstemp()
tempobj = os.fdopen(tempfd, 'wb')
tempobj.write('hello, world')
tempobj.close()
os.remove(tempfn)
time.sleep(10)
def worker_notempfile(index):
tempfn = str(index) + '.txt'
# The values I'm passing os.open may be different than tempfile.mkstemp
# uses, but it works this way as does using the open() function to create
# a file object directly.
tempfd = os.open(tempfn,
os.O_EXCL | os.O_CREAT | os.O_TRUNC | os.O_RDWR)
tempobj = os.fdopen(tempfd, 'wb')
tempobj.write('hello, world')
tempobj.close()
os.remove(tempfn)
time.sleep(10)
def main():
for count in range(NUM_THREADS):
if count % 100 == 0:
print('Opening thread %s' % count)
wthread = threading.Thread(target=worker_tempfile)
#wthread = threading.Thread(target=worker_notempfile, args=(count,))
started = False
while not started:
try:
wthread.start()
started = True
except thread.error:
print('failed starting thread %s; sleeping' % count)
time.sleep(3)
if __name__ == '__main__':
main()
, 그것은 완료 될 때까지 실행됩니다.
다른 방법 주변 (worker_tempfile
사용) 나는 다음과 같은 오류가 발생합니다 : 내가 잘못
$ python threadtempfiletest.py
Opening thread 0
Opening thread 100
Opening thread 200
Opening thread 300
Exception in thread Thread-301:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 460, in __bootstrap
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 440, in run
File "threadtempfiletest.py", line 17, in worker_tempfile
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tempfile.py", line 302, in mkstemp
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tempfile.py", line 236, in _mkstemp_inner
OSError: [Errno 24] Too many open files: '/var/folders/4L/4LtD6bCvEoipksvnAcJ2Ok+++Tk/-Tmp-/tmpJ6wjV0'
어떤 아이디어 뭘하는지? 이것은 파이썬의 버그입니까, 아니면 제가 골치 썩은 것입니까?
업데이트 2009-12-14 : 나는 그 대답을 찾았지만 나는 그것을 좋아하지 않는다고 생각합니다. 아무도이 문제를 재현 할 수 없었기 때문에 나는 사무실을 돌아 다니며 기계를 사냥하러 갔다. 그것은 내 기계를 제외한 모든 것을 통과시켰다. 나는 내가 사용했던 것과 동일한 소프트웨어 버전으로 Mac에서 테스트했다. 나는 심지어 똑같은 하드웨어 및 소프트웨어 구성으로 데스크탑 G5를 사냥하러 갔다. 같은 결과였다. 두 테스트 모두 (임시 파일과 임시 파일 제외) 모든 것에 성공했습니다.
킥 들어, 내가 파이썬 2.6.4를 다운로드하고 내 데스크톱에서, 그리고 파이썬 2.5.1로 내 시스템에서 동일한 패턴을 시도 : tempfile이 실패하고 notempfile가 성공했습니다.
이것은 내 맥에서 무언가가 떠들썩하다는 결론을 이끌어 내고 있지만, 나는 무엇을 알아 내지 못했다. 어떤 제안이라도 환영합니다.
좋은 코드는 명확한 질문으로 +1 할 수는 없습니다. –
파이썬 버전을 제공해 줄 수 있습니까? 나는 그것이 중요한지는 모르지만 그것은 가능할 것이다. –
Jonathan : 대단히 감사합니다! Peter : Python 2.5.1. 또한이를 반영하기 위해 질문을 편집했습니다. – Schof