0

I'm trying to execute multiple linux commands using Python. Currently my script is

    import os
    os.system('cmd1')
    os.system('cmd2')
    os.system('cmd3')
    os.system('cmd4')

I'm wondering if there's a way to execute cmd1 and cmd2 in parallel. After cmd1 and cmd2 finishes, the system will execute cmd3 and cmd4? Thanks!

  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Jul 28 '17 at 21:30
  • 1
    More to the point, your browser search should have given you a hit for the [multiprocessing](https://stackoverflow.com/questions/3842237/parallel-processing-in-python#3846686) package. – Prune Jul 28 '17 at 21:31
  • Actually, [subprocess](https://docs.python.org/2/library/subprocess.html) should also work and be enough to spawn them in parallel. See this answer: https://stackoverflow.com/questions/9743838/python-subprocess-in-parallel – Carlo Mazzaferro Jul 28 '17 at 21:39

1 Answers1

0

Use os.fork() and run the two system commands in two children. Use os.waitpid() on the two children before executing cmd3.

madscientist159
  • 550
  • 4
  • 12