如何同时执行多个python文件?

2024-04-29 02:29:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想同时跟踪多个文件并将日志推送到scribe。 我从一个配置文件中读取文件,然后我想跟踪每个文件并将日志发送给scribe。 我尝试的是只为第一个文件发送日志,而不为其他文件发送日志。在

我希望对每个文件同时运行tailing,并同时发送每个文件的日志。在

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

Tags: 文件sizelineseekopenfilenamewhereprotocol
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))

@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()是问题中代码的副本:

^{pr2}$

follow()可以使用FS监视工具实现,请参见tail -f in python with no time.sleep。在

相关问题 更多 >