-2

I have a computational program written in Python using Ray package with the following output:

 Actor(Play001,69a6825d641b461327313d1c01000000)

This process uses the following pid:

pid = 87972

In the Ray dashboard I can view the logs. Snippets is as follows:

Logs

192.168.0.101 (PID: 87972)

1 Function 1: Starting up
2 Worker 1: Done
3 Press enter to continue or to exit

In Python I managed to check if this PID exists:

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

What I want is in real time to display the logs as well in my terminal output. How do I do this?

  • Would you like to see the logs remotely, or on the same machine? Also, could you provide your code? It will be easier to help you. See [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Miguel Alorda Apr 17 '21 at 06:18
  • 1
    On the same machine; I've posted a solution. If you got one better please post it. – jrrysprng02 Apr 17 '21 at 06:19
  • Are you launching your script from the command line? If so, you could just use a pipe to input the stdout of your script somewhere else – Miguel Alorda Apr 17 '21 at 06:32
  • That is indeed the case – jrrysprng02 Apr 17 '21 at 06:33

2 Answers2

1

On ray, the driver process outputs on its stdout logs from all other workers. If you want to process your scripts logs on another script, you can use a pipe.

Let's say you have two files: myrayscript.py and mylogparser.py

On myrayscript.py you have your script, just as it was written before.

On mylogparser you will receive from stdin ray logs:

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

Now, to use a pipe, from the command line:

python3 myrayscript.py | python3 mylogparser.py
Miguel Alorda
  • 623
  • 5
  • 13
  • The code hangs. When pressing ctrl+c: KeyboardInterrupt Exception ignored in: <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> BrokenPipeError: [Errno 32] Broken pipe – jrrysprng02 Apr 17 '21 at 06:47
0

The following solution works although cumbersome:

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

For a bit more completeness you can watch for changes in the file.

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