Non-Blocking Calls

It is possible to run a command using a non-blocking code. This gives you more fine grained control over the input and output. Similarly to blocking code, there are async_run() and async_exec() methods.

These methods return an instance of Process which represents a running process. You can write to stdin or iterate over stdout and stderr which are line-based generators.

Warning

Both stdout and stderr read the process outputs line by line, therefore they will block until a full line is read.

This is especially a problem in the executed program prompts for input. It is better to use expect() or expect_nobody() for interactive programs.

Example: Non-Blocking call
from pytest_mh import MultihostHost
from pytest_mh.conn import ProcessResult

@pytest.mark.topology(...)
def test_hello(example: ExampleRole) -> None:
    process = example.host.conn.async_run("cat")

    process.stdin.write("Hello\n")
    assert next(process.stdout) == "Hello"

    process.stdin.write("World\n")
    assert next(process.stdout) == "World"

    result = process.wait()