Python asyncio Streams and Subprocesses/Run a shell command asynchronously

Spawn a shell pipeline with `create_subprocess_shell()`.

Section: Subprocesses

Run a shell command asynchronously

python
python
import asyncio

async def main():
    proc = await asyncio.create_subprocess_shell(
        "echo one && echo two",
        stdout=asyncio.subprocess.PIPE,
    )
    stdout, _ = await proc.communicate()
    print(stdout.decode())

asyncio.run(main())
Explanation

Convenient for shell pipelines, but avoid untrusted input.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Subprocesses
Run a subprocess with exec
Capture stdout and stderr asynchronously.
OpenIn sheetpythonsame section
Open a TCP connection
Use stream reader and writer objects.
Run blocking I/O in a thread
Use `asyncio.to_thread()` for simple thread offloading.
Start a TCP server
Serve client connections with async handlers.
Use `run_in_executor()`
Offload work with explicit loop executor integration.