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 beawait’d. Run a processprocwith the arguments*args, optionally piping data (write) into the process’s stdin - then returns the stdout and stderr of the process.By default,
stdoutandstdinare set toasyncio.PIPEwhile stderr defaults toasyncio.STDOUT. You can override these by passing new values as keyword arguments.While it’s recommended to use the file descriptor types from the
asynciomodule, they’re generally just aliases to the types insubprocess, meaningsubprocess.PIPEshould work the same asasyncio.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
- Key stdout
The subprocess file descriptor for stdout, e.g.
asyncio.PIPEorasyncio.STDOUT- Key stderr
The subprocess file descriptor for stderr, e.g.
asyncio.PIPEorasyncio.STDOUT- Key stdin
The subprocess file descriptor for stdin, e.g.
asyncio.PIPEorasyncio.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