Python Asyncio Subprocess [Asynchronous Subprocesses]

Asyncio, a Python library, empowers asynchronous handling of subprocesses. It’s vital for concurrent execution of external commands or processes within asynchronous programs.

By using asyncio, you execute these processes without blocking your program’s flow. Asyncio employs coroutines, allowing tasks to pause and resume efficiently, giving the illusion of parallelism while optimizing system resources.

It facilitates non-blocking execution, enabling multiple subprocesses to work simultaneously.

Additionally, asyncio offers completion monitoring, error handling, and resource efficiency.

Overall, asyncio’s subprocess management capabilities enhance program efficiency and responsiveness by running external tasks concurrently in an asynchronous environment.

Async Subprocess Python Examples

Example 1

Below is an example of how to use asyncio with subprocesses:

import asyncio

async def run_command(command):
    process = await asyncio.create_subprocess_shell(
        command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE
    )
    
    stdout, stderr = await process.communicate()
    
    return process.returncode, stdout, stderr

async def main():
    # Define the commands you want to run asynchronously
    commands = [
        "echo 'Hello, World'",
        "ls -l",
        "sleep 2 && echo 'Done sleeping'"
    ]

    # Run the commands concurrently
    tasks = [run_command(command) for command in commands]
    results = await asyncio.gather(*tasks)

    # Print the results
    for i, (returncode, stdout, stderr) in enumerate(results):
        print(f"Command {i+1}:")
        print(f"Return Code: {returncode}")
        print(f"STDOUT:\n{stdout.decode()}")
        print(f"STDERR:\n{stderr.decode()}\n")

if __name__ == "__main__":
    asyncio.run(main())
Code language: Python (python)

In this example:

  1. The run_command function is defined to execute a shell command as a subprocess asynchronously. It uses create_subprocess_shell to create a subprocess, capturing its stdout and stderr streams.
  2. The main function defines a list of commands you want to run concurrently. It then creates a list of tasks using a list comprehension, where each task represents the execution of a command.
  3. asyncio.gather is used to run the tasks concurrently, and it collects the results.
  4. Finally, the results are printed, including the return code, standard output, and standard error for each command.

By using asyncio, you can execute multiple subprocesses concurrently, which can be very useful for I/O-bound or CPU-bound tasks in asynchronous Python programs.

Example 2

To run subprocesses asynchronously in Python using the asyncio library, you can use the asyncio.create_subprocess_exec() or asyncio.create_subprocess_shell() functions to create subprocesses, and then use await to execute them. Here’s an example of how to use asyncio with asynchronous subprocesses:

import asyncio

async def run_command():
    # Define the command you want to run asynchronously
    command = "echo 'Hello, World'"
    
    # Create a subprocess
    process = await asyncio.create_subprocess_shell(
        command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE
    )
    
    # Wait for the subprocess to complete
    await process.wait()
    
    # Read the output
    stdout, stderr = await process.communicate()
    
    return process.returncode, stdout.decode(), stderr.decode()

async def main():
    # Run the command asynchronously
    returncode, stdout, stderr = await run_command()

    print(f"Return Code: {returncode}")
    print(f"STDOUT:\n{stdout}")
    print(f"STDERR:\n{stderr}")

if __name__ == "__main__":
    asyncio.run(main())
Code language: Python (python)

In this example:

  1. The run_command function defines the shell command you want to run asynchronously. It creates a subprocess using create_subprocess_shell and specifies that you want to capture the stdout and stderr streams of the subprocess.
  2. The await process.wait() line is used to wait for the subprocess to complete.
  3. After the subprocess completes, await process.communicate() is used to read the output from stdout and stderr.
  4. The main function is defined to run the command asynchronously and then print the return code, standard output, and standard error.
  5. Finally, the asyncio.run(main()) line is used to run the main function using the asyncio event loop.

This code will execute the specified shell command asynchronously and print the results. You can replace the command variable with any other shell command you want to run asynchronously.

How do I execute an external command asynchronously in Python?

To run an external command asynchronously from Python, you can use the asyncio library in combination with the subprocess module. Here’s a step-by-step guide on how to do it:

  1. Import the necessary modules:
import asyncio
import subprocess
Code language: Python (python)
  1. Define an asynchronous function to run the external command:
async def run_external_command(command):
    process = await asyncio.create_subprocess_shell(
        command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE
    )
    
    stdout, stderr = await process.communicate()
    
    return process.returncode, stdout, stderr
Code language: Python (python)

In this function, await asyncio.create_subprocess_shell() is used to create a subprocess that runs the specified command. We capture the stdout and stderr streams of the subprocess.

  1. Create an asynchronous main function to run your command:
async def main():
    command = "your_external_command_here"
    returncode, stdout, stderr = await run_external_command(command)
    
    print(f"Return Code: {returncode}")
    print(f"Standard Output:\n{stdout.decode()}")
    print(f"Standard Error:\n{stderr.decode()}")

if __name__ == "__main__":
    asyncio.run(main())Code language: Python (python)

Replace "your_external_command_here" with the actual external command you want to run asynchronously.

  1. Finally, execute the main function using the asyncio.run() function.

This code will run the external command asynchronously, capturing its standard output and standard error. It will then print the return code, standard output, and standard error to the console.

Make sure you have the asyncio library available in your Python environment. You can install it if it’s not already installed using pip:

pip install asyncio
Code language: Python (python)

Remember that the ability to run a command asynchronously is especially useful when you have multiple tasks or subprocesses to manage concurrently, allowing your program to remain responsive while executing potentially time-consuming external commands.

Read More;

    by
  • Aniket Singh

    Aniket Singh holds a B.Tech in Computer Science & Engineering from Oriental University. He is a skilled programmer with a strong coding background, having hands-on experience in developing advanced projects, particularly in Python and the Django framework. Aniket has worked on various real-world industry projects and has a solid command of Python, Django, REST API, PostgreSQL, as well as proficiency in C and C++. He is eager to collaborate with experienced professionals to further enhance his skills.

Leave a Comment