Fahadkalis
Fahadkalis

Reputation: 3261

Use subprocess in Python

I am writing a small program in Python, to record an audio WITH printing some text at same time.

But my Print is executed until finishing of audio recording. Can you please help me to resolve this issue?

import picamera, subprocess, os, sys

a1 = "arecord -f cd -D plughw:0 -d 10 a.wav"
subprocess.call(a1,shell= True)
print("Audio record is only for 10sec")

Upvotes: 0

Views: 1208

Answers (1)

Reut Sharabani
Reut Sharabani

Reputation: 31339

You're using subprocess.call, which blocks:

Run the command described by args. Wait for command to complete, then return the returncode attribute.

You can use a Popen object, which doesn't block:

proc = subprocess.Popen(a1.split())
# code will proceed

# use proc.communicate later on

Or you can have two things run separately using a Thread (which then spawns a process in it's own context):

import picamera, subprocess, os, sys
import threading

def my_process():
    a1 = "arecord -f cd -D plughw:0 -d 10 a.wav"
    subprocess.call(a1,shell= True)
thread = threading.Thread(target=my_process)
thread.start()
print("Audio record is only for 10sec")

Upvotes: 3

Related Questions