call_sys_async

async privex.helpers.asyncx.call_sys_async(proc, *args, write: Union[bytes, str] = None, **kwargs) → Tuple[bytes, bytes][source]

Async version of call_sys() - works exactly the same, other than needing to be await’d. Run a process proc with the arguments *args, optionally piping data (write) into the process’s stdin - then returns the stdout and stderr of the process.

By default, stdout and stdin are set to asyncio.PIPE while stderr defaults to asyncio.STDOUT. You can override these by passing new values as keyword arguments.

While it’s recommended to use the file descriptor types from the asyncio module, they’re generally just aliases to the types in subprocess, meaning subprocess.PIPE should work the same as asyncio.PIPE.

Simple Example:

>>> from privex.helpers import call_sys_async, stringify
>>> # All arguments are automatically quoted if required, so spaces are completely fine.
>>> folders, _ = await call_sys_async('ls', '-la', '/tmp/spaces are fine/hello world')
>>> print(stringify(folders))
total 0
drwxr-xr-x  3 user  wheel  96  6 Dec 17:46 .
drwxr-xr-x  3 user  wheel  96  6 Dec 17:46 ..
-rw-r--r--  1 user  wheel   0  6 Dec 17:46 example

Piping data into a process:

>>> data = "hello world"
>>> # The data "hello world" will be piped into wc's stdin, and wc's stdout + stderr will be returned
>>> out, _ = await call_sys_async('wc', '-c', write=data)
>>> int(out)
11
Parameters
  • proc (str) – The process to execute.

  • args (str) – Any arguments to pass to the process proc as positional arguments.

  • write (bytes|str) – If this is not None, then this data will be piped into the process’s STDIN.

Key stdout

The subprocess file descriptor for stdout, e.g. asyncio.PIPE or asyncio.STDOUT

Key stderr

The subprocess file descriptor for stderr, e.g. asyncio.PIPE or asyncio.STDOUT

Key stdin

The subprocess file descriptor for stdin, e.g. asyncio.PIPE or asyncio.STDIN

Key cwd

Set the current/working directory of the process to this path, instead of the CWD of your calling script.

Return tuple output

A tuple containing the process output of stdout and stderr