相关疑难解决方法(0)

在'with open()'块内使用'while True'循环会带来什么后果?

例如:

def read_file(f):
    with open(f, 'r') as file_to_read:
        while True:
            line = file_to_read.readline()
            if line:
                yield line
            else:
                time.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)

生成器由另一个函数使用:

def fun_function(f):
    l = read_file(f)
    for line in l:
        do_fun_stuff()
Run Code Online (Sandbox Code Playgroud)

一个用例将是读取一个无限更新的文本文件,例如日志,其中每秒钟左右添加新行。

据我了解read_file(),只要有结果,该功能就会阻止其他功能。但是由于除非文件中没有新行,否则什么也不做,在这种情况下,这似乎还可以。我的问题是,是否还有其他原因不喜欢这种阻止模式(例如性能)?

python blocking

6
推荐指数
0
解决办法
85
查看次数

检测日志文件旋转(在监视日志文件进行修改的同时)

我使用以下代码跟踪ssh登录:

def follow(thefile):
  thefile.seek(0,2)
  while True:
    line = thefile.readline()
    if not line:
      time.sleep(0.1)
      continue
    yield line

if __name__ == '__main__':
  logfile = open('/var/log/auth.log', 'r')
  loglines = follow(logfile)
  for line in loglines:
    print 'do something here'
Run Code Online (Sandbox Code Playgroud)

我注意到该脚本在几天后突然停止工作。我没有任何错误,它不会终止,只会停止工作,好像readline()永远不会返回。

所以我执行了a echo 'test' >> auth.log.1,这的确是被脚本处理了,因为前一段时间auth.log将其重命名为auth.log.1

如何跟踪这种日志轮换的时间并进行相应调整?

python linux logging

5
推荐指数
1
解决办法
2812
查看次数

使用Python进行Syslog实时监控

我在ubuntu的syslog文件中有日志。我可以查看来自python的新消息,还是始终需要打开/关闭syslog文件?谢谢

python ubuntu logging monitoring syslog

2
推荐指数
1
解决办法
4896
查看次数

Python:如何让脚本进入和退出minicom终端?

为了输入minicom并保存它的日志,我使用"sudo minicom -C nameoffile",但是我想在循环中执行此操作,打开minicom可以通过使用子进程来完成但是我找不到任何东西来退出我的minicom循环并继续循环,因为你需要输入"ctrl-a,然后x"或"ctrl-a,然后q",之后必须按回车确认这一点.有人有任何想法或建议吗?

python linux bash loops subprocess

0
推荐指数
1
解决办法
5139
查看次数

标签 统计

python ×4

linux ×2

logging ×2

bash ×1

blocking ×1

loops ×1

monitoring ×1

subprocess ×1

syslog ×1

ubuntu ×1