0

Looks like the job=subprocess.Popen, job.pid not giving the right PID, instead PID+1 seems to work

I checked the thread: Python subprocess.popen wrong pid python - subprocess.Popen().pid return the pid of the parent script

but none of them really answered my question.

my working (it is said the successful rate of ‘PID+1’ is 99%, I ddidn't fail yet, but felt the possibility) code is follows:

def Running(PID):
    try:
        os.kill(PID, 0)
    except OSError:
        return False
    else:
        return True

job=subprocess.Popen('cfl3d_seq<cfl3d.inp &', shell=True)
Runs.append(int(job.pid)+1)
print(int(job.pid)+1)
print(Runs)
time.sleep(10)

while len(Runs)>=TotalRun:
    for run in Runs:
        if Running(run)==False:
            print('kill ', run)
            Runs.remove(run)
    time.sleep(60)

This is running a external software in the background. Is there any better clean way to get the right PID?

If it is running python code itself, is there any better way to do this?

I didn't use any fork thread when using time.sleep(), will this sleep the external cfl3d software?

if what executed through Popen() is python code, will this sleep the popened python?

Thank you very much!

Westack
  • 105
  • 5
  • Not using `shell=True` would give you the correct PID of the child process. However, that would require you to pass yout `stdin` input explicitly from your Python script to the subprocess. – JohanL Aug 08 '19 at 21:46
  • Thanks Johanl! How to do that? I tried simply remove shell=True, which will not work. – Westack Aug 09 '19 at 01:26
  • It depends on how `cfl3d_seq` accepts inputs. Does the file name have to come through `stdin`? – JohanL Aug 09 '19 at 17:50
  • I am not pretty sure about that, but every time when I do it manually, I input through the stdin (typing it through xterminal), did I got your meanning? – Westack Aug 10 '19 at 00:31
  • No, what I mean is the addition of the `<`(less than) character, which is redirecting `stdin`to be whatever is on the right side. – JohanL Aug 11 '19 at 12:31
  • Thanks, JohanL, I don't understand! The code is as exactly as shown, the right side only got cfl3d.inp & – Westack Aug 20 '19 at 01:39

0 Answers0