user60679
user60679

Reputation: 729

Wait all subprocess to complete in python

I have a method which executes a command using subprocess, I want to call that method "n" no of times and wait for all "n" subprocess to complete for example:

import subprocess
class mysubprocess():
    def child_process(self,directory):
        self.process=subprocess.Popen('ls',cwd=directory)

    def execute(self):
        directory=['/home/suresh/Documents','/home/suresh/Downloads']
        for i in directory:
            print(i)
            self.child_process(directory)

        self.process.wait()

def main():
    myobject=mysubprocess()
    myobject.execute()

if __name__=='main':
    main()

Upvotes: 0

Views: 721

Answers (1)

falsetru
falsetru

Reputation: 369074

You need to store references to the Popen objects to call wait methods of them later. (The code in the question overwrites the Popen object with the last Popen object, and waits only the last sub-process.)

import subprocess


class mysubprocess():

    def execute(self, directory_list):
        procs = []
        for d in directory:
            print(d)
            procs.append(subprocess.Popen('ls', cwd=d))  # <---
        for proc in procs:
            proc.wait()


def main():
    myobject = mysubprocess()
    myobject.execute(['/home/suresh/Documents','/home/suresh/Downloads'])


if __name__ == '__main__':
    main()

Other issues

  • The code is passing the entire list (directory) instead of item.
  • The last if statement should compare __name__ with '__main__'.

Upvotes: 1

Related Questions