我有一個多線程函數,它們都寫入同一個日誌文件。我怎樣才能使這個函數(也許用一個函數裝飾器)將寫入日誌文件的執行添加到隊列中。 小例子:爲一個函數建立一個隊列,所以它只能在Python中一次運行一次
#!/usr/bin/python
import thread
import time
# Define a function for the thread
def print_time(threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
writeToLog(threadName, time.ctime(time.time()))
print "%s: %s" % (threadName, time.ctime(time.time()))
# Create two threads as follows
try:
thread.start_new_thread(print_time, ("Thread-1", 2,))
thread.start_new_thread(print_time, ("Thread-2", 4,))
except:
print "Error: unable to start thread"
def writeToLog(threadName, time):
self.fileWriter = open("log.txt", "w")
self.fileWriter.write("ThreadName: " + threadName + "\n")
self.fileWriter.write("Time: " + time + "\n")
self.fileWriter.close()
我怎樣才能讓執行時該功能writeToLog添加到隊列中?現在每當兩個線程都調用writeToLog函數時出錯,因爲其他writeToLog函數(來自其他線程)已經關閉了該文件。有這個作家,這是在結束時關閉一個全局變量的時候,我得到的輸出是這樣的:
ThreadName: thread1
ThreadName: thread2
Time: 9:50AM
Time: 9:50AM
和輸出我總是想有這樣的:
ThreadName: Thread-1
Time: 9:50AM
ThreadName: Thread-2
Time: 9:50AM
爲什麼不使用python'logging',這是設計成線程安全的? –
@RapolasK。我是python的新手,從來沒有見過這個模塊,看起來相當方便。我想我現在將使用這個模塊而不是我自己的類。謝謝! – Jetse