user3535074
user3535074

Reputation: 1338

asyncio call works in cmd but not in python

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?

Upvotes: 0

Views: 274

Answers (1)

user3535074
user3535074

Reputation: 1338

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")

Upvotes: 1

Related Questions