Python Forum
How to get the PID from a process at first
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the PID from a process at first
#1
Question 
I have a question about how to get the PID from a process at first, not when the process has finished.

This is because I want to be able to kill the FFmpeg process (not the python script) if necessary, so it doesn't make sense to know its PID at the end.

FYI: This script is getting the PIDs from FFmpeg processes.

Below you will see how I coded this script, which is working fine, but I get the PID at the end, as I mentioned before.

Any idea about how to do it?
import json, base64, sys, subprocess

thisList = json.loads(base64.b64decode(sys.argv[1]))
logFileName = json.loads(base64.b64decode(sys.argv[2]))

p = subprocess.Popen(thisList, stderr = open(logFileName, 'w'))
print(p.pid)
As you can see, I am decoding an encoded base64 string (the FFmpeg command line) to protect it because is coming from an URL.

Also, I need to write the FFmpeg output to a file, so I am getting the stderr to write it externally. I encoded its path in base64 too.

Basically, I am trying to give the freedom to the user to kill FFmpeg if the process is taking longer than expected, so I need the PID quickly.

Finally to mention that there will be many concurrent FFmpeg PIDs working at the same time, so something like searching the PID using the FFmpeg name is too generic.

It could be possible to launch a second script to search the entire command line of FFmpeg and getting its PID, which could work well in Unix (I already have a script to do that in PHP), but not in Windows. I would like to be compatible in both SO.

Thank you very much in advance.
Reply
#2
I don't understand what you're seeing. You have access to p (and therefore p.pid) as soon as the Popen returns, and that does not require that the process exit. It may still be running. You don't have to wait for it to exit.

Even without the pid, you can try to kill the process with p.terminate()

Here I run a simple shell script that would otherwise run forever. It includes the PID in the ouput. The same PID is reported from the python program and the program is killed after 5 seconds.

import subprocess
import time

cmd = ["/bin/bash", "-c", 'while [ 1 ] ; do echo -n "$$ " ; date ; sleep 1 ; done']

p = subprocess.Popen(cmd, stdout = open("/tmp/stdout", "w"))
print(f"Process pid is {p.pid}")
time.sleep(5)
print(f"Current process status is {p.poll()} (None means process is still running)")
print("Terminating...")
p.terminate()
time.sleep(1)  # Wait a second for process to terminate
print(f"Current process status is {p.poll()}")
Output:
Process pid is 3431 Current process status is None (None means process is still running) Terminating... Current process status is -15 $ cat /tmp/stdout 3431 Sun May 23 21:27:05 PDT 2021 3431 Sun May 23 21:27:06 PDT 2021 3431 Sun May 23 21:27:07 PDT 2021 3431 Sun May 23 21:27:08 PDT 2021 3431 Sun May 23 21:27:09 PDT 2021
Reply
#3
Therefore the problem is that I am calling the python script from PHP, and because of that I am receiving the PID when the script has finished.

I have to figure out how to call the script from PHP to get that PID as soon as python print it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,658 Sep-03-2019, 09:49 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020