0

The Following Command is Not Waiting for The Subprocess to Finish The Execution.

mylist = ['First','Second','Third',....., 'Nth']
for items in mylist:
        subprocess.run('googleimagesdownload --keywords "'+items+'" --size medium --limit 100  --output_directory ../../../downloads')

I Want To Execute The Above Python Script in Linux Terminal But It is Not Waiting For a Subprocess To Complete Each Iteration

for items in mylist:
        os.system('googleimagesdownload --keywords "'+items+'" --size medium --limit 100  --output_directory ../../../downloads')
        

The Following Python Script is Working Perfectly Fine on Windows It Waits for The Command To Execute Completely Then Move to The Next Iteration But Didn't Works on Linux Terminal

4
  • the subprocess.run() call should wait for the call to finish, does it work if you replace your function call with subprocess.run(['sleep', '1'])? Commented Jul 13, 2021 at 15:49
  • After Putting The Following Code Below Mine Now The Process Reruns After 1 SecondsNothing Else. Commented Jul 13, 2021 at 16:12
  • Why do you want to wait for each task to finish? You could download them in parallel. If your problem is that your program ends before it even finishes, you should consider using asyncio stackoverflow.com/a/63786058/277267 Commented Jul 13, 2021 at 17:11
  • Can You Please Share The Example With My Code ? with Asyncio Commented Jul 14, 2021 at 5:48

1 Answer 1

0

I think the simplest way for you is using subprocess.Popen(). You can use then wait() method to wait for ending of called command.

Code to show you how it works:

import subprocess
mylist = ['sleep 1']*3 # program will sleep for 3 seconds
for items in mylist:
        process = subprocess.Popen(items,shell = True)
        process.wait()
        print('waiting... ')
print('FINISH!')

Your code modified and using subprocess.Popen():

mylist = ['First','Second','Third',....., 'Nth']
for items in mylist:
        process = subprocess.Popen('googleimagesdownload --keywords "'+items+'" --size medium --limit 100  --output_directory ../../../downloads')
        process.wait()
Sign up to request clarification or add additional context in comments.

3 Comments

Still After Putting Your Modified Code, Program is Still Not Waiting for Each Iteration To Completely Execute The Command & Wait For It To End.!
I used to use Linux programs from google to GCP. Very often programs started running in the background and after call and pressed enter key. Next command could be passed regardless of executing previous. Maybe your Linux program is running in the background..
Then Why It is Not Working Here ? Any Other Solution ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.