Categories
Python Answers

How to use subprocess.Popen to connect multiple processes by pipes with Python?

Spread the love

Sometimes, we want to use subprocess.Popen to connect multiple processes by pipes with Python.

In this article, we’ll look at how to use subprocess.Popen to connect multiple processes by pipes with Python.

How to use subprocess.Popen to connect multiple processes by pipes with Python?

To use subprocess.Popen to connect multiple processes by pipes with Python, we can use the subprocess.PIPE property.

For instance, we write

import subprocess

some_string = b'input_data'

sort_out = open('outfile.txt', 'wb', 0)
sort_in = subprocess.Popen('sort', stdin=subprocess.PIPE, stdout=sort_out).stdin
subprocess.Popen(['awk', '-f', 'script.awk'], stdout=sort_in, 
                 stdin=subprocess.PIPE).communicate(some_string)

to pipe the sort_out file’s content to sort with subprocess.PIPE.

And then we pipe the result from sort to awk with subprocess.PIPE.

Conclusion

To use subprocess.Popen to connect multiple processes by pipes with Python, we can use the subprocess.PIPE property.

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 *