1

I have for loop in python in each iteration of the loop I want to run a bash script and after its termination continue the loop and again run the bash script:

for batch in something:
    proc = Popen(['./mybash.sh'])
    proc.wait()

The mybash.sh script will compute something and display a value using echo. However, when I run this code it seems that it executes mybash.sh script only once, since I can only see the value displayed by echo only for the first iteration. What is wrong with the code? BTW, I am using python 3.5

pierre
  • 139
  • 1
  • 2
  • 9
  • Could you check the return value about `proc.wait()` – Xiwei Wang Dec 19 '18 at 03:17
  • 1
    I can't reproduce your error. I looped over a simple hello world script as you described above, and it worked as expected. – CrepeGoat Dec 19 '18 at 03:21
  • Can you clarify where you are seeing the output of your script? Does it open up a new console when you run your script? Can you try routing your `stdout` to a `subprocess.PIPE` and change `proc.wait()` to `print(proc.communicate())`. It is supposed to print out the output of your script. – najeem Dec 19 '18 at 03:57
  • You should ask the question about the problem you actually have… – LtWorf Dec 19 '18 at 09:12
  • @Xiwei Wang print(proc.wait()) outputs 0 – pierre Dec 20 '18 at 13:45
  • @najeem in each iteration of the loop there are other operations which have been performed on GPU and they produce a text file on the disc. Then it is the Popen(['./mybash.sh']), and my bash script reads that text file and computes some score and then just use `echo` to show that score. – pierre Dec 20 '18 at 13:51
  • @LtWorf the problem is it only outputs once and does not show anything for the rest of the iterations of for-loop. When I discard proc.wait(), it outputs the same value for every iterations. – pierre Dec 20 '18 at 14:54

2 Answers2

0

The reason of it seems that it executes mybash.sh script only once maybe cause by:

  • Your iteration run only once.
  • The proc.wait() does not return.
  • The ./mybash.sh not work correctly.

And you can change you script as this:

for batch in something:
    # to check the looping times
    print(batch)
    proc = Popen(['./mybash.sh'])
    # to check if the mybash.sh exit normally
    print(proc.wait())
Xiwei Wang
  • 346
  • 4
  • 17
0

Popen merely starts a process. You probably want something like

from subprocess import run, PIPE

for batch in something:
    print(run(['./mybash.sh'],
        stdout=PIPE, stderr=PIPE,
        check=True, universal_newlines=True).stdout)

The check=True assumes your script returns a useful and valid exit code.

For (much!) more, see https://stackoverflow.com/a/51950538/874188

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Didn't work how? It still stops, or you don't see the output? – tripleee Dec 21 '18 at 05:08
  • 1
    It does not show the output, I have seen this post [Only first subprocess.Popen(…, stdin=f) in a loop works correctly](https://stackoverflow.com/questions/50866386/only-first-subprocess-popen-stdin-f-in-a-loop-works-correctly) but I am not sure how relevant is to my problem – pierre Dec 21 '18 at 10:09
  • Does your script expect to receive some input? What is it actually doing? – tripleee Dec 21 '18 at 10:11