Categories
Python Answers

How to execute cat subprocess in parallel with Python?

Spread the love

Sometimes, we want to execute cat subprocess in parallel with Python.

In this article, we’ll look at how to execute cat subprocess in parallel with Python.

How to execute cat subprocess in parallel with Python?

To execute cat subprocess in parallel with Python, we can use the subprocess module.

For instance, we write:

from subprocess import Popen

processes = [
    Popen("echo {i:d}; sleep 2; echo {i:d}".format(i=i), shell=True)
    for i in range(5)
]
exitcodes = [p.wait() for p in processes]

We call Popen with the command we want to run`.

And we set shell to True to let us use the shell.

We specify that we run the commands 5 times with for i in range(5).

Then we return exit codes for each process with [p.wait() for p in processes].

Conclusion

To execute cat subprocess in parallel with Python, we can use the subprocess module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *