同时启动两个子过程

时间:2016-03-24 14:19:32

标签: python audio subprocess record popen

我尝试使用命令arecord同时启动2个音频记录(使用2个不同的麦克风)。

首先,这是我用来从一个麦克风(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()

然后,我用subprocess.Popen启动另一个程序,它调用record.py两次:

# #!/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()

这些功能(终止,等待和结束)都不允许同时启动2条记录。

0 个答案:

没有答案
相关问题