如何将经过的秒数附加到每行的前面,因为它来自 grep 子进程?
How to appending elapsed seconds to the front of each line as it comes out of grep subprocess?
我正在尝试将经过的秒数附加到 grep 输出的每一行,但我遇到了一些问题。
另一个挑战是我想 运行 其他代码,而 top,grep 进程在后台 运行ning。 (所以实现必须是非阻塞的)
目前,由于 SO 上的另一个答案,我已经能够附加当前时间。在我的示例代码中,我使用的是 'perl' 但它不需要,尽管我不能使用 gawk/ts 或任何其他在 MacOS 上不默认的 utilities/commands。
最高输出示例
1227 Google Chrome He 0.0 00:06.59 13 1 151 58M 0B 51M 793 793 sleeping *0[5] 0.00000 0.00000 502 35702 2182 20972 10027 34224
写入文本文件的所需输出示例(假设经过了 1.59 秒)
1.59 1227 Google Chrome He 0.0 00:06.59 13 1 151 58M 0B 51M 793 793 sleeping *0[5] 0.00000 0.00000 502 35702 2182 20972 10027 34224
文件中的下一行应该有另一个时间较晚的顶部输出(假设下一个顶部命令从初始开始时间起 2.13 秒从缓冲区中出来)
2.13 1227 Google Chrome He 0.0 00:06.59 13 1 151 58M 0B 51M 793 793 sleeping *0[5] 0.00000 0.00000 502 35702 2182 20972 10027 34224
到目前为止我的代码
from timeit import default_timer as timer
start_time = timer()
f = open('file.txt', 'w')
top_proc = subprocess.Popen(['top'], stdout=subprocess.PIPE)
grep_proc = subprocess.Popen(['grep', '--line-buffered', ' Chrome'], stdin=top_proc.stdout, stdout=subprocess.PIPE)
# Somehow append elapsed time since start_time to the grep output with this other process (it would be appending `timer() - start_time`)
# Currently it just appends the current time
perl_proc = subprocess.Popen(['perl', '-pe', 'use POSIX strftime; print strftime "%H:%M:%S ", localtime'], stdin=grep_proc.stdout, stdout=f)
# Other code that runs for indefinite amount of time
other_code()
top_proc.send_signal(signal.SIGINT)
top_proc.wait()
grep_proc.wait()
perl_proc.wait()
f.close()
我正在使用 timeit 模块,因为根据我的研究,它更准确,而且我的代码是用于对 Chrome 进程的内存使用情况进行基准测试的脚本的一部分。我尝试查看 perl 命令,但很难找到任何示例或类似用例。
有没有一种方法可以重新调整我的 perl 子进程的用途以附加经过的时间?或者还有什么其他方法可以实现我的目标?
更新
我试过perl_proc = subprocess.Popen(['perl', '-pe', 'print "' + str(time.time() - start_time) + '" '], stdin=grep_proc.stdout, stdout=f)
,但它似乎只给我初始行的经过时间。
您为什么要使用子流程来完成 Python 中简单可用的事情?
import time
import subprocess
start_time = time.time()
f = open('file.txt', 'w')
top_proc = subprocess.Popen(['top'], stdout=subprocess.PIPE)
for line in top_proc.stdout:
if b'Chrome' in line:
print( "%.3f" % (time.time() - start_time), line )
但是,top
并非设计为由此调用。您的行将包含终端控制字符。
而且,顺便说一下,awk
在 MacOS 中肯定可用,就像完整的普通 Unix 命令集一样。
程序包 GNU moreutils
中的程序 ts
(时间戳)完全符合您的要求。
示例:
for i in a b c d e; do echo $i; sleep 1; done | ts -s %.s
打印
0.000016 a
0.944115 b
1.974076 c
3.002769 d
4.031955 e
不过,我同意 :在 python 中添加时间戳是可行的方法。
我正在尝试将经过的秒数附加到 grep 输出的每一行,但我遇到了一些问题。
另一个挑战是我想 运行 其他代码,而 top,grep 进程在后台 运行ning。 (所以实现必须是非阻塞的)
目前,由于 SO 上的另一个答案,我已经能够附加当前时间。在我的示例代码中,我使用的是 'perl' 但它不需要,尽管我不能使用 gawk/ts 或任何其他在 MacOS 上不默认的 utilities/commands。
最高输出示例
1227 Google Chrome He 0.0 00:06.59 13 1 151 58M 0B 51M 793 793 sleeping *0[5] 0.00000 0.00000 502 35702 2182 20972 10027 34224
写入文本文件的所需输出示例(假设经过了 1.59 秒)
1.59 1227 Google Chrome He 0.0 00:06.59 13 1 151 58M 0B 51M 793 793 sleeping *0[5] 0.00000 0.00000 502 35702 2182 20972 10027 34224
文件中的下一行应该有另一个时间较晚的顶部输出(假设下一个顶部命令从初始开始时间起 2.13 秒从缓冲区中出来)
2.13 1227 Google Chrome He 0.0 00:06.59 13 1 151 58M 0B 51M 793 793 sleeping *0[5] 0.00000 0.00000 502 35702 2182 20972 10027 34224
到目前为止我的代码
from timeit import default_timer as timer
start_time = timer()
f = open('file.txt', 'w')
top_proc = subprocess.Popen(['top'], stdout=subprocess.PIPE)
grep_proc = subprocess.Popen(['grep', '--line-buffered', ' Chrome'], stdin=top_proc.stdout, stdout=subprocess.PIPE)
# Somehow append elapsed time since start_time to the grep output with this other process (it would be appending `timer() - start_time`)
# Currently it just appends the current time
perl_proc = subprocess.Popen(['perl', '-pe', 'use POSIX strftime; print strftime "%H:%M:%S ", localtime'], stdin=grep_proc.stdout, stdout=f)
# Other code that runs for indefinite amount of time
other_code()
top_proc.send_signal(signal.SIGINT)
top_proc.wait()
grep_proc.wait()
perl_proc.wait()
f.close()
我正在使用 timeit 模块,因为根据我的研究,它更准确,而且我的代码是用于对 Chrome 进程的内存使用情况进行基准测试的脚本的一部分。我尝试查看 perl 命令,但很难找到任何示例或类似用例。
有没有一种方法可以重新调整我的 perl 子进程的用途以附加经过的时间?或者还有什么其他方法可以实现我的目标?
更新
我试过perl_proc = subprocess.Popen(['perl', '-pe', 'print "' + str(time.time() - start_time) + '" '], stdin=grep_proc.stdout, stdout=f)
,但它似乎只给我初始行的经过时间。
您为什么要使用子流程来完成 Python 中简单可用的事情?
import time
import subprocess
start_time = time.time()
f = open('file.txt', 'w')
top_proc = subprocess.Popen(['top'], stdout=subprocess.PIPE)
for line in top_proc.stdout:
if b'Chrome' in line:
print( "%.3f" % (time.time() - start_time), line )
但是,top
并非设计为由此调用。您的行将包含终端控制字符。
而且,顺便说一下,awk
在 MacOS 中肯定可用,就像完整的普通 Unix 命令集一样。
程序包 GNU moreutils
中的程序 ts
(时间戳)完全符合您的要求。
示例:
for i in a b c d e; do echo $i; sleep 1; done | ts -s %.s
打印
0.000016 a
0.944115 b
1.974076 c
3.002769 d
4.031955 e
不过,我同意