1

The most answer I met are stdout.readlines().

But some process never end, outputing log forever, so readlines() is blocked.

Of course, while xx.stdout.readline(): or while xx.stdout.read(1): or stdout.read() has the same problem.

For the read method is always blocked if there's no data in pipe(?file), is there a method like read(timeout=xxx) or read(max_len=xxx) or stdout.isAvalable etc, or can stdout be converted to a stream like object(which read method return -1 if no data available)

Example:

import subprocess
p = subprocess.Popen(['tail', '-f', 'test.log'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
time.sleep(10)
p.stdout.readlines() # blocked here, for `tail` watches the file forever and never exits
Xixiaxixi
  • 159
  • 8
  • can you check the answer in this post - https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python – anuragal Nov 29 '19 at 05:42

0 Answers0