2009-11-11 2 views

답변

7

RRDTool과 같은 좋은 소리입니다.

하지만 파이썬을 고수하고 싶다면 내 프로그램에 데이터를 스트리밍하려면 꼬리를 사용하면됩니다 (파일이 계속 작성되었다고 가정 할 경우 Python에서 곧바로 open()이 작동합니다).

tail -F data.log | python myprogram.py 

myprogram.py 같은 것을 볼 수 있었다 :

import sys 

p = ... # create a pylab plot instance 
for line in sys.stdin: 
    elements = line.split(',') # or whatever separator your file has in it 
    p.add(element[0], element[1]) # add data to the pylab plot instance 
2

요한이 언급 한 바와 같이, 입력 할 수 있습니다 파일에 꼬리 출력,하지만 당신은 몇 가지 이유는 파일에서 모든 것을 처리하고 싶어 인한 경우 다소 동적 인 그래프의 예를 원한다면, 여기에

import math 
import time 
import pylab 

def getDataTest(filePath): 
    s = 0 
    inc = .05 
    x_list=pylab.arange(0, 5.0, 0.01) 
    while 1: 
     s += inc 
     if abs(s) > 1: 
      inc=-inc 

     y_list = [] 
     for x in x_list: 
      x += s 
      y = math.cos(2*math.pi*x) * math.exp(-x) 
      y_list.append(y) 

     yield x_list, y_list 

def tailGen(filePath): 
    f = open(filePath) 
    #f.seek(0, 2) # go to end 
    for line in f: yield line 
    while 1: 
     where = f.tell() 
     line = f.readline() 
     if line: 
      yield line 
     else: 
      time.sleep(.1) 
      f.seek(where) 

def getData(filePath): 
    x_list = [] 
    y_list = [] 
    maxCount = 10 
    for line in tailGen(filePath): 
     # get required columns 
     tokens = line.split(",") 
     if len(tokens) != 2: 
      continue 
     x, y = tokens 
     x_list.append(x) 
     y_list.append(y) 
     if len(x_list) > maxCount: 
      x_list = x_list[-maxCount:] 
      y_list = x_list[-maxCount:] 
      yield x_list, y_list 

pylab.ion() 
pylab.xlabel("X") 
pylab.ylabel("Y") 

dataGen = getData("plot.txt") # getDataTest("plot.txt") # 
x_list, y_list = dataGen.next() 
plotData, = pylab.plot(x_list, y_list, 'b') 
#pylab.show() 
pylab.draw() 
for (x_list, y_list) in dataGen: 
    time.sleep(.1) 
    plotData, = pylab.plot(x_list, y_list, 'b') 
    pylab.draw() 

당신은 요소를 픽업 할 수 있으며 문제가 해결 될 것이라고 생각합니다.

1

다음은 tail'er, 필터 (gawk) 및 플로터 (python)의 세 부분으로 구성된 유닉스 파이프입니다.

tail -f yourfile.log | gawk '/PCM1/{print $21; fflush();}' | python -u tailplot.py 

여기 파이썬 스크립트입니다. 1 (y) 또는 2 (x y) 개의 데이터 열을 공급할 수 있습니다. gawk을 사용하지 않으면 버퍼링을 비활성화하는 방법을 알아야합니다. 예 : sed -u.

pa-poca$ cat ~/tailplot.py 

import math 
import time 
import sys 
import pylab 

pylab.ion() 
pylab.xlabel("X") 
pylab.ylabel("Y") 

x = [] 
y = [] 
counter = 1 
while True : 
    line = sys.stdin.readline() 
    a = line.split() 
    if len(a) == 2: 
     x.append(a[0]) 
     y.append(a[1]) 
    elif len(a) == 1: 
     x.append(counter) 
     y.append(a[0]) 
     counter = counter + 1 
    pylab.plot(x, y, 'b') 
    pylab.draw() 
관련 문제