0

I am using sox to retrieve audio file information. There are 2 windows cmd commands which return information correctly:

C:\Users\Me> "path\to\sox.exe" "--i" "path\to\audiofile.wav"

C:\Users\Me> "path\to\sox.exe" "path\to\audiofile.wav" "-n" "stat" 

I'm using an asyncio script to run these two commands and collect the data for processing. I use the below code:

async def async_subprocess_command(*args):
    # Create subprocess
    process = await asyncio.create_subprocess_exec(
        *args,
        # stdout must a pipe to be accessible as process.stdout
        stdout=asyncio.subprocess.PIPE)
    # Wait for the subprocess to finish
    stdout, stderr = await process.communicate()

    # Return stdout
    return stdout.decode().strip()

data1 = await async_subprocess_command(soxExecutable, "--i", audiofilepath)
data2 = await async_subprocess_command(soxExecutable, audiofilepath,"-n", "stat")

As both the cmd commands act as expected, I am confused that data2 from the python script is always blank. Data1 is as expected (with data from sox).

Can anyone help me understand why?

2
  • The second command seems to write to stderr. Commented Aug 16, 2017 at 7:27
  • Thats a great idea - I hadn't thought of that. I had to add an stderr=asyncio.subprocess.PIPE parameter to the asyncio.create_subprocess_exec too and now I can see that it's coming back in the sterr value. Thanks a lot Vincent! Commented Aug 16, 2017 at 12:40

1 Answer 1

1

For some reason the second command returns its result via sterr. I added the additional parameter to the asyncio.create_subprocess_exec function to connect sterr to the asyncio.subprocess.PIPE.

   async def async_subprocess_command(*args):
        # Create subprocess
        process = await asyncio.create_subprocess_exec(
            *args,
            # stdout must a pipe to be accessible as process.stdout
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE)
        # Wait for the subprocess to finish
        stdout, stderr = await process.communicate()

        # Return stdout and sterr
        return stdout.decode().strip(),sterr.decode().strip()

    data1a, data1b = await async_subprocess_command(soxExecutable, "--i", audiofilepath)
    data2a, data2b = await async_subprocess_command(soxExecutable, audiofilepath,"-n", "stat")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.