asyncio.create_subprocess_shell asynchronously starts a subprocess by executing a command through a system shell such as zsh or bash. It returns a Process object that can be awaited, inspected, or terminated, allowing an asyncio application to reuse command-line programs without reimplementing them. section 13.1
Use the shell deliberately:
- Prefer asyncio.create_subprocess_exec when shell functionality is unnecessary, because shell availability and configuration can differ between machines. section 13.1 section 13.3
- Choose create_subprocess_shell when a command depends on shell features such as the pipe operator. section 13.1.2
- Pipe standard input, output, or error when interaction is required, consume piped output to avoid deadlocks, and use communicate to send input. section 13.3
- Limit many concurrent subprocesses with a semaphore when system-resource contention becomes a concern. section 13.3
Running a shell pipeline
A command such as echo 'encrypt this!' | gpg -c --batch --passphrase 3ncryptm3 --cipher-algo TWOFISH requires shell pipeline support. The shell form can execute the pipe, whereas create_subprocess_exec cannot provide that operator directly. section 13.1.2
echo 'encrypt this!' | gpg -c --batch --passphrase 3ncryptm3 --cipher-algo TWOFISH
Shows the concrete gpg pipeline that motivates choosing shell execution. section 13.1.2
