1

I'm trying to launch 2 audio records simultaneously (with 2 different mics) using the command arecord.

First, this is the code I use to record from one mic (record.py) :

#!/usr/bin/env python2
# -*-coding:Latin-1 -*

from threading import Thread
import pyaudio, sys, wave, time, commands, os

# classe qui permet d'exécuter un second Thread qui va enregistrer en même temps
class Record(Thread):
    def __init__(self, device):
        super(Record, self).__init__()
        self.device = device
    def run(self):
        # permet d'enregistrer en spécifiant le / les bons micros, et enregistre un fichier à la bonne date et heure, en spécifiant le numéro du micro
        u = commands.getoutput('arecord -D plughw:' + self.device + ',0 -f cd -t wav -d 10 --use-strftime %Y/%m/%d/mic' + self.device + '-listen-%H-%M-%v.wav')

class ActionsPerMic(Thread):
    def __init__(self, device):
        super(ActionsPerMic, self).__init__()
        self.device = device

    def run(self):
        rec = Record(self.device)
        rec.start()
        rec.join()

# pour un micro branché au Raspberry Pi
mic1 = ActionsPerMic(sys.argv[1])
mic1.start()
mic1.join()

Then, I launch another program with subprocess.Popen which calls record.py twice:

# #!/usr/bin/env python2
# # -*-coding:Latin-1 -*

import commands, time, subprocess, signal, os, pyaudio, sys, wave
from subprocess import Popen
from threading import Thread

listDevices = list()
p = pyaudio.PyAudio()

dev0 = ["python", "record.py", "3"] 
dev1 = ["python", "record.py", "4"] 
pDev0 = subprocess.Popen(dev0, stdin = subprocess.PIPE)
pDev1 = subprocess.Popen(dev0, stdin = subprocess.PIPE)
# pDev0.terminate()
# pDev1.terminate()
# pDev0.wait()
# pDev1.wait()
pDev0.finish()
pDev1.finish()

None of these functions (terminate, wait and finish) are permitted to launch the 2 records simultaneously.

IanB
  • 3,489
  • 1
  • 20
  • 24
Utopia
  • 278
  • 1
  • 3
  • 19
  • 1
    When you start a process with `subprocess.Popen()` then it *already is* running "in the background" regardless of what the Python interpreter is doing. Your `wait()` calls aren't influencing that. This only stops the Python interpreter from continuing (but not any other process). So I'm not sure exactly what you problem is or why you think you can't "launch the 2 records simultaneously"? – Martin Tournoij Mar 24 '16 at 15:30
  • The problem is when I'm launching the subprocesses with none of the functions terminate, wait of finish, nothing happens. That means the two records are not made. When I use the functions terminate or wait, nothing happens too. And with finish, error "'Popen' object has no attribute 'finish'" occured, so the function doesn't exist. – Utopia Mar 24 '16 at 15:38
  • Okay. So are you sure that this subprocess calls are actually correct? For example, what happens when you run `python record.py 3` from the shell? Does that work? And what happens if you run just one subprocess? Does that work? I also noticed just now that both the `Popen()` calls use `dev0` as the first argument (this should be `dev1` for the second call). Perhaps this is the problem? – Martin Tournoij Mar 24 '16 at 15:42
  • Thanks you for your answer. Yes I had seen that this was an error when I'm calling dev0 and not dev1. I tried to record with arecord from the shell and I needed to be root. So when I'm launching the code in superuser mode, everything it's fine. – Utopia Mar 24 '16 at 15:45
  • related: [Python threading multiple bash subprocesses?](http://stackoverflow.com/q/14533458/4279) – jfs Mar 25 '16 at 13:12

0 Answers0