从程序中的for循环中保存数据?

时间:2017-01-13 18:51:10

标签: python python-2.7 python-3.x subprocess

说,我有一个应用程序日志文件,它逐行增加

Jan 13 20:44:25 Think process1: ID1: Request received user=user1
Jan 13 20:44:26 Think process1: ID2: Request received user=user2
Jan 13 20:44:27 Think process2: ID1: user accessed file=file1
Jan 13 20:44:28 Think process1: ID3: Request received user=user3
Jan 13 20:44:29 Think process3: ID1: Request Served token=tok1
Jan 13 20:44:30 Think process2: ID2: user accessed file=file2
Jan 13 20:44:31 Think process3: ID2: Request Served token=tok2
Jan 13 20:44:32 Think process2: ID3: user accessed file=file3
.....
Jan 13 20:59:24 Think process3: ID**N**: user accessed file=file**N**

我的代码如下

from sh import tail

def received(input_message):
    user1 = input_message[2].split('=')
    user = user1[1].replace('\n', '')
    return(user)

def accessed(input_message):
    file1 = input_message[2].split('=')
    file = file1[1].replace('\n', '')
    return(file)

def served(input_message):
    tok1 = input_message[2].split('=')
    tok = tok1[1].replace('\n', '')
    return(tok)

for line in tail("-f", "/opt/jagan/app.log", _iter=True):
    column = line.split(' ')
    date = column[0] + ' ' + column[1] + ' ' + column[2]
    host = column[3]
    process = column[4]
    ID = column[5]
    message = column[6:]
    if "process1" in process:
         username = received(message)
         print("Username is:" +(username))
    if "process2" in process:
         filename = accessed(message)
         print("filename is:" +(filename))
    if "process3" in process:
         token = served(message)
         print("Token is:" +(token))

在此,ID对于每个请求都是唯一的。它将经历所有过程。 因此,当一个请求完成时,我想在提供请求时打印所有必需的数据。

像:

>['user1','file1','tok1']
['user**N**','file**N**','tok**N**']

问题1.如何在程序中存储来自for循环的数据?我不会将数据存储在文件或某个数据库中。因为,日志生成太高,会影响这个程序的性能。

问题2.如何在请求完成时触发包含请求所有数据的操作?我想在请求完成时触发请求的所有信息。说我想用可变数据创建变量列表,也不想在其他地方保存数据。如果程序停止,数据也会丢失(我没关系)。

2 个答案:

答案 0 :(得分:1)

logs=[]
for line in tail...
    ...
    logs.append([filename, username, token])

答案 1 :(得分:0)

问题1:

要保存所需数据,您可以使用字典或列表,只要调用服务(如果我理解正确,那就是数据保存代码的最后一个阶段)。

您应该指定一个global scope变量,以保存它:

示例:

data_list = []
data_dict = {} # Use the ID as a key, and all values of data as the value

问题2:

如果要对收到的数据执行function(操作),请在完成收集数据时再次调用数据作为参数的函数(同样,在调用服务时):

示例:

def parse_data(line):
    pass # Your parsing code

# And your function call
for line in tail("-f", "/opt/jagan/app.log", _iter=True):
...
if "process3" in process:
     token = served(message)
     parse_data(line)
     print("Token is:" +(token))
...