问题描述
我有一个使用 Ray 包用 Python 编写的计算程序,输出如下:
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 上,驱动程序处理所有其他工作人员在其标准输出日志上的输出。如果你想在另一个脚本上处理你的脚本日志,你可以使用管道。
假设您有两个文件:myrayscript.py
和 mylogparser.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)