Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python如何对多个文件并发执行此代码?_Python - Fatal编程技术网

使用python如何对多个文件并发执行此代码?

使用python如何对多个文件并发执行此代码?,python,Python,我想同时跟踪多个文件,并将日志推送到scribe。 我从配置文件中读取文件,然后我想跟踪每个文件并将日志发送给scribe。 我尝试的是只发送第一个文件的日志,而不发送其他文件的日志 我想同时运行每个文件的跟踪,并同时发送每个文件的日志 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]

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

我想同时运行每个文件的跟踪,并同时发送每个文件的日志

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()
试试这样的


不同的执行方式:

其中
log\u 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监控工具实现,请参阅。

这不会同时跟踪所有文件。您的问题似乎与本章末尾给出的示例非常相似。整个演示非常有趣,但在第7部分中,作者给出了一个使用线程和队列跟踪多个日志的示例。这不是一个完整的答案(抱歉!),但您可能需要查看一下。使用此选项,它只将第一个日志条目发送给scribe,随后的日志条目将显示,但不会发送给scribescribe@Rishabh:是否为多个文件同时调用了
log\u entry()
(如果不确定,请添加调试输出)?如果从单个线程在循环中调用它,
log\u entry()
是否有效(
for i in range(10):log\u entry('test',str(i))
?多线程(用假数据替换
follow()
以测试它)?当我跟踪抄写员制作的日志时,它们都工作正常
#!/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()
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()