如何从终端中的PID接收实时日志?



我有一个用Python编写的计算程序,使用Ray包,输出如下:

Actor(Play001,69a6825d641b461327313d1c01000000)

此过程使用以下pid:

pid = 87972

在Ray面板中,我可以查看日志。代码段如下:

Logs
192.168.0.101 (PID: 87972)
1 Function 1: Starting up
2 Worker 1: Done
3 Press enter to continue or to exit

在Python中,我设法检查了这个PID是否存在:

import psutil
pid = 87972
if psutil.pid_exists(pid):
print("a process with pid %d exists" % pid)

我想要的是在我的终端输出中实时显示日志。我该怎么做?

在ray上,驱动程序处理来自所有其他工作程序的stdout日志输出。如果你想在另一个脚本上处理你的脚本日志,你可以使用管道。

假设您有两个文件:myrayscript.pymylogparser.py

myrayscript.py上,您可以得到您的脚本,就像以前写的一样。

mylogparser上,您将收到来自stdin的光线日志:

while True:
logLine = input()
# do your stuff here

现在,要使用管道,请从命令行:

python3 myrayscript.py | python3 mylogparser.py

以下解决方案虽然很麻烦,但仍然有效:

pid = 87972
p = psutil.Process(pid)   
temp = p.open_files()
sPth = temp[0][0]
oFile = open(sPth)
Content = oFile.read()
msContent = Content.splitlines()

为了更完整,您可以查看文件中的更改。

from watchgod import watch
for changes in watch(sPth):
print(changes)

最新更新