Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Process subprocess.Popen with Pipe

I'm trying to record and convert a stream with python using arecord.

In the shell this is a command more or less like:

arecord -B 5000 -f dat | lame -m j -q 5 -V 2 - test.mp3 &

In Python I tried this with subprocess.Popen:

reccmd = ["arecord", "-B", "5000", "-f", "dat"]
mp3cmd = ["lame", "-m", "j", "-q", "5", "-V", "2", "-", "test.mp3"]
p = subprocess.Popen(reccmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(mp3cmd, stdin=p.stdout)
p2.communicate()

But I don't know how to include the "&" or something similar in the code to be able to stop the recording with a

killall arecord command

like image 611
Max Marwin Avatar asked Apr 07 '12 21:04

Max Marwin


People also ask

How do you use subprocess with pipes?

To use a pipe with the subprocess module, you have to pass shell=True . In your particular case, however, the simple solution is to call subprocess. check_output(('ps', '-A')) and then str. find on the output.

What is pipe in Popen?

The popen() function executes the command specified by the string command. It creates a pipe between the calling program and the executed command, and returns a pointer to a stream that can be used to either read from or write to the pipe.

What does subprocess pipe mean?

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions: os.


1 Answers

If you don't want the main Python process to block while executing the subprocesses, simply don't call p2.communicate(). The calls to subprocess.Popen() don't block.

like image 89
Sven Marnach Avatar answered Sep 28 '22 19:09

Sven Marnach