我想要同時拖放多個文件並將日誌推到劃線上。 我正在從配置文件中讀取文件,然後我想要拖尾每個文件並將日誌發送給抄寫員。 我試過的是發送日誌只有第一個文件,而不是其他人。使用python如何同時爲多個文件執行此代碼?
我想同時爲每個文件運行拖尾並同時爲它們中的每一個發送日誌。
for l in Config.items('files'):
print l[0]
print l[1]
filename = l[1]
file = open(filename,'r')
st_results = os.stat(l[1])
st_size = st_results[6]
file.seek(st_size)
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
category=l[0]
message=line
log_entry = scribe.LogEntry(category, message)
socket = TSocket.TSocket(host='localhost', port=1463)
transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
client = scribe.Client(iprot=protocol, oprot=protocol)
transport.open()
result = client.Log(messages=[log_entry])
transport.close()
你的問題看起來非常類似於[關於使用python生成器進行系統編程的演示文稿](http://www.dabeaz.com/generators-uk/GeneratorsUK.pdf)結束時的示例。整個演示文稿非常有趣,但在第7部分中,作者給出了一個使用線程和隊列來尾錄多個日誌的示例。這不是一個完整的答案(對不起!),但你可能想看看它。 – 2011-12-21 12:55:26