2011-12-21 22 views
1

我想要同時拖放多個文件並將日誌推到劃線上。 我正在從配置文件中讀取文件,然後我想要拖尾每個文件並將日誌發送給抄寫員。 我試過的是發送日誌只有第一個文件,而不是其他人。使用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() 
+0

你的問題看起來非常類似於[關於使用python生成器進行系統編程的演示文稿](http://www.dabeaz.com/generators-uk/GeneratorsUK.pdf)結束時的示例。整個演示文稿非常有趣,但在第7部分中,作者給出了一個使用線程和隊列來尾錄多個日誌的示例。這不是一個完整的答案(對不起!),但你可能想看看它。 – 2011-12-21 12:55:26

回答

1

不同的實現的@Pengman's idea

#!/usr/bin/env python 
import os 
import time 
from threading import Thread 

def follow(filename): 
    with open(filename) as file: 
     file.seek(0, os.SEEK_END) # goto EOF 
     while True: 
      for line in iter(file.readline, ''): 
       yield line 
      time.sleep(1) 

def logtail(category, filename): 
    print category 
    print filename 
    for line in follow(filename): 
     print line, 
     log_entry(category, line) 

for args in Config.items('files'): 
    Thread(target=logtail, args=args).start() 

哪裏log_entry()是從問題的代碼的副本:

def log_entry(category, message): 
    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=[entry]) 
    transport.close() 

follow()可以使用FS監視工具來實現,參見tail -f in python with no time.sleep

+0

使用它它只發送第一個日誌條目抄寫和後續日誌條目顯示,但不發送給抄寫 – Rishabh 2011-12-21 13:41:33

+1

@Rishabh:是否爲多個文件同時調用log_entry()(如果您不確定,請添加調試輸出) ?如果你從一個線程('for i in range(10):log_entry('test',str(i))'')中調用它,'log_entry()多個線程(用假數據替換'follow()'來測試它)? – jfs 2011-12-21 22:52:19

+0

它的工作原理感謝一個負載.... – Rishabh 2011-12-22 07:26:54

2

嘗試這樣的事情(Inspired by this)

import threading 

def monitor_file(l): 

    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() 


for l in Config.items('files'): 
    thread = threading.Thread(target=monitor_file, args=(l)) 
+0

這不會尾巴所有的文件在同一時間 – Rishabh 2011-12-21 10:29:01

+0

你需要[開始線程](http://stackoverflow.com/a/8589113/4279)。 – jfs 2011-12-21 11:34:13

相關問題