liveBook · Manning

Asyncio.create_subprocess_shell

As explained in Python Concurrency with asyncio

asyncio.create_subprocess_shell is an asyncio function that asynchronously starts a subprocess by executing a command through a system shell.

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

How Python Concurrency with asyncio covers it

How deep it goes
Defines shell execution, contrasts it with exec execution, and addresses streams, communication, termination, and portability cautions.
Examples
Uses a gpg encryption pipeline requiring the shell pipe operator.
What you can do after
Operate command-line programs asynchronously and select shell execution when pipelines or other shell features are required.
How it connects
Connects shell execution with asyncio.create_subprocess_exec, Process, pipes, communicate, semaphores, and command-line applications.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.