Title: Theory / Unimportant / CS / Programming / Python / Async / Process
Tags: constant
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html

Instruction: asyncio.create_subprocess_exec(
  program,
  *args,
  stdin=None,
  stdout=None,
  stderr=None,
  limit=None,
  **kwds
)
Effect: coroutine Create a subprocess.
Language: python
Extra: The limit argument sets the buffer limit for StreamReader wrappers for Process.stdout and Process.stderr (if subprocess.PIPE is passed to stdout and stderr arguments).
Return a Process instance.
See the documentation of loop.subprocess_exec() for other parameters.

Instruction: syncio.create_subprocess_shell(
cmd,
stdin=None,
stdout=None,
stderr=None,
limit=None,
**kwds
)
Language: python
Returns: coroutine Run the cmd shell command.
Extra: The limit argument sets the buffer limit for StreamReader wrappers for Process.stdout and Process.stderr (if subprocess.PIPE is passed to stdout and stderr arguments).
Return a Process instance.
See the documentation of loop.subprocess_shell() for other parameters.
Assuming: Important It is the application’s responsibility to ensure that all whitespace and special characters are quoted appropriately to avoid shell injection vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and special shell characters in strings that are going to be used to construct shell commands.

Constant: asyncio.subprocess.PIPE
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Language: python
Context: subprocess
Assuming: Important It is the application’s responsibility to ensure that all whitespace and special characters are quoted appropriately to avoid shell injection vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and special shell characters in strings that are going to be used to construct shell commands.

Constant: stderr = asyncio.subprocess.STDOUT
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Language: python
Context: subprocess
Assuming: Important It is the application’s responsibility to ensure that all whitespace and special characters are quoted appropriately to avoid shell injection vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and special shell characters in strings that are going to be used to construct shell commands.

Constant: asyncio.subprocess.DEVNULL
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Language: python
Context: subprocess
Assuming: Important It is the application’s responsibility to ensure that all whitespace and special characters are quoted appropriately to avoid shell injection vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and special shell characters in strings that are going to be used to construct shell commands.

Typ: asyncio.subprocess.Process
Meaning: An object that wraps OS processes created by the create_subprocess_exec() and create_subprocess_shell() functions.
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Language: python
Context: subprocess
Assuming: Important It is the application’s responsibility to ensure that all whitespace and special characters are quoted appropriately to avoid shell injection vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and special shell characters in strings that are going to be used to construct shell commands.

Question: What's the difference between asyncio.subprocess.Process and subprocess.Popen
Answer: unlike Popen, Process instances do not have an equivalent to the poll() method;
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Variables: process: asyncio.subprocess.Process
Context: subprocess
Answer2: the communicate() and wait() methods don’t have a timeout parameter: use the wait_for() function;
Answer3: the Process.wait() method is asynchronous, whereas subprocess.Popen.wait() method is implemented as a blocking busy loop;
Answer4: the universal_newlines parameter is not supported.

Instruction: process.wait()
Effect: coroutine Wait for the child process to terminate. Set the return code attribute
Language: python
Context: subprocess
Returns: the returncode attribute.
Variables: process: asyncio.subprocess.Process
Extra: This method can deadlock when using stdout=PIPE or stderr=PIPE and the child process generates so much output that it blocks waiting for the OS pipe buffer to accept more data. Use the communicate() method when using pipes to avoid this condition.

Instruction: process.communicate(input=None)
Effect: send input to stdin if not None.
Language: python
Context: subprocess
Returns: (stdout_data, stderr_data).
Variables: process: asyncio.subprocess.Process
Extra: If either BrokenPipeError or ConnectionResetError exception is raised when writing input into stdin, the exception is ignored. This condition occurs when the process exits before all data are written into stdin.
If it is desired to send data to the process’ stdin, the process needs to be created with stdin=PIPE. Similarly, to get anything other than None in the result tuple, the process has to be created with stdout=PIPE and/or stderr=PIPE arguments.
Note, that the data read is buffered in memory, so do not use this method if the data size is large or unlimited.
Use the communicate() method rather than process.stdin.write(), await process.stdout.read() or await process.stderr.read. This avoids deadlocks due to streams pausing reading or writing and blocking the child process.
Time complexity: coroutine: wait for process to terminate.

Instruction: process.send_signal(signal)
Effect: Sends the signal signal to the child process.
Language: python
Context: subprocess
Variables: process: asyncio.subprocess.Process
Extra: On Windows, SIGTERM is an alias for terminate(). CTRL_C_EVENT and CTRL_BREAK_EVENT can be sent to processes started with a creationflags parameter which includes CREATE_NEW_PROCESS_GROUP.

Instruction: process.terminate()
Effect: Stop the child process.
On POSIX systems this method sends signal.SIGTERM to the child process.
On Windows the Win32 API function TerminateProcess() is called to stop the child process.
Language: python
Context: subprocess
Variables: process: asyncio.subprocess.Process

Instruction: process.kill()
Effect: Kill the child process.
On POSIX systems this method sends SIGKILL to the child process.
On Windows this method is an alias for terminate().
Language: python
Context: subprocess
Variables: process: asyncio.subprocess.Process

Variable: process.stderr
Meaning: Standard error stream ( StreamReader ) or None if the process was created with stderr=None.
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Context: subprocess
Language: python
Variables: process: asyncio.subprocess.Process

Variable: process.stdout
Meaning: Standard output stream ( StreamReader ) or None if the process was created with stdout=None.
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Context: subprocess
Language: python
Variables: process: asyncio.subprocess.Process

Variable: process.stdin
Meaning: Standard input stream (StreamWriter) or None if the process was created with stdin=None.
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Context: subprocess
Language: python
Variables: process: asyncio.subprocess.Process

Variable: process.pid
Meaning: Process identification number.
Note that for processes created by the create_subprocess_shell() function, this attribute is the PID of the spawned shell.
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Context: subprocess
Language: python
Variables: process: asyncio.subprocess.Process

Variable: process.returncode
Meaning: Return code of the process when it exits.
A None value indicates that the process has not terminated yet.
A negative value -N indicates that the child was terminated by signal N (POSIX only).
Page: https://docs.python.org/3.10/library/asyncio-subprocess.html
Context: subprocess
Language: python
Variables: process: asyncio.subprocess.Process

Typ: asyncio_queue.SendfileNotAvailableError
Meaning: The “sendfile” syscall is not available for the given socket or file type.
Page: https://docs.python.org/3.10/library/asyncio-queue.html
Language: python
Extra: This exception can be caught to perform custom operations when asyncio Tasks are cancelled. In almost all situations the exception must be re-raised.

Variable: incomplete_read_error.expected
Meaning: The total number (int) of expected bytes.
Page: https://docs.python.org/3.10/library/asyncio-queue.html
Language: python
Variables: incomplete_read_error: asyncio_queue.IncompleteReadError