약 4 개의 입력 텍스트 파일을 읽고이를 모두 하나의 개별 파일에 쓰고 싶습니다. 두 개의 스레드를 사용하므로 더 빠르게 실행됩니다!
여기에 내 질문과 파이썬의 코드입니다 :
- 각 스레드는 "writeInFile"함수 안에 "lines"과 같은 변수의 버전을 가지고 있습니까?
2 - Tutorialspoint에서 코드의 일부를 복사했기 때문에 마지막 줄에 "while 1 : pass"가 무엇인지 이해하지 못합니다. 설명 할 수 있니? http://www.tutorialspoint.com/python/python_multithreading.htm
3 - 스레드에 대한 지연 시간은 중요합니까?
4 약 400 개의 입력 텍스트 파일이 있고 각각의 파일을 별도의 파일에 작성하기 전에 몇 가지 작업을 수행하려는 경우 몇 개의 스레드를 사용할 수 있습니까?
5- 스레드를 10 개 사용한다고 가정하면 다른 폴더 (각각 40 개의 입력 텍스트 파일이있는 10 개의 폴더)에 입력하는 것이 더 좋으며 각 스레드는 하나의 폴더를 호출하거나 아래 코드에서 이미 수행 한 것을 사용합니다. 이전에 다른 스레드에서 읽지 않은 경우 각 스레드가 400 입력 텍스트 파일 중 하나를 읽도록 요청 했습니까? 파이썬에서 여러 스레드로 하나의 메소드를 호출하십시오.
processedFiles=[] # this list to check which file in the folder has already been read by one thread so the other thread don't read it
#Function run by the threads
def writeInFile(threadName, delay):
for file in glob.glob("*.txt"):
if file not in processedFiles:
processedFiles.append(file)
f = open(file,"r")
lines = f.readlines()
f.close()
time.sleep(delay)
#open the file to write in
f = open('myfile','a')
f.write("%s \n" %lines)
f.close()
print "%s: %s" % (threadName, time.ctime(time.time()))
# Create two threads as follows
try:
f = open('myfile', 'r+')
f.truncate()
start = timeit.default_timer()
thread.start_new_thread(writeInFile, ("Thread-1", 0,))
thread.start_new_thread(writeInFile, ("Thread-2", 0,))
stop = timeit.default_timer()
print stop - start
except:
print "Error: unable to start thread"
while 1:
pass
답장을 보내 주셔서 감사합니다. –