Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch command-context manager on top of run-context manager #532

Conversation

christian-monch
Copy link
Contributor

This PR is a Draft-PR because it is against another PR.

This PR adds a batchcommand-context manager. A batchcommand-context manager starts a subprocess and supports batch-style communication with it. That means, the user can send a request to the subprocess and receive the response from the subprocess.

How bytes that are read from the subprocess are assembled into a response is determined by the protocol that is used when the batchcommand-context manager is created. Typical responses would be single lines of text or JSON-encoded data.

The PR contains three convenience functions that provide context-manager for common interaction patterns (common in datalad and datalad_next anyways):

  1. stdout_batchcommand: this context-manager writes to stdin of the subprocess and returns the next, yet unread, chunk of bytes that was written to stdout of the subprocess.
  2. stdoutline_batchcommand: this context-manager writes to stdin of the subprocess and returns the next, yet unread, line that was written to stdout of the subprocess. Line-separators and whether they are included in the output can be chosen when creating the context-manager.
  3. annexjson_batchcommand: this context-mananger writes to stdin of the subprocess and returns a JSON-object, that is created from the next, yet unread, line that is written to stdout of the subprocess. This context-mananger is geared towards git-annex batch commands with JSON-result output, i.e. git-annex invocations with the parameters --batch, --json, or --json-error-messages.

The tests contain an example for a protocol, PythonProtocol that handles output from an interactive python interpreter, which varies in size.

Co-authored-by: Michael Hanke <[email protected]>

This commit adds a `batchcommand`-context-manager that
keeps track of a subprocess and supports batch-style
communication. That means, it allows to send a request
to the subprocess and will return the response from the
subprocess. How the output of the subprocess has to be
interpreted to yield a response is defined by the
protocol that is provided to the
batchcommand-context-manager. The
batchcommand-context-manager uses the run-context-manager
to ensure that the exit-handler waits for process
termination.

The commit also contains three convenience functions
that provide context-manager for common interaction
patterns (common in `datalad` and `datalad_next` anyways):

1. `stdout_batchcommand`: this context-manager writes to
   `stdin` of the subprocess and returns the next, yet
   unread, chunk of bytes that was written to `stdout`
   of the subprocess.

2. `stdoutline_batchcommand`: this context-manager writes
   to `stdin` of the subprocess and returns the next,
   yet unread, line that was written to `stdout` of the
   subprocess. Line-separators and whether they are
   included in the output can be chosen when creating
   the context-manager.

3. `annexjson_batchcommand`: this context-mananger writes
   to `stdin` of the subprocess and returns a JSON-object,
   that is created from the next, yet unread, line that
   is written to `stdout` of the subprocess. This
   context-mamanger is geared towards git-annex batch
   commands with JSON-result output, i.e. git-annex
   invocations with the parameters `--batch`, `--json`,
   `--json-error-messages`.

The tests provide an example for protocol that handles
output that varies in size, i.e. output of
python-programs. The class that implements the protocol
is called `PythonProtocol`.
@mih
Copy link
Member

mih commented Nov 23, 2023

With #538 merged, we are betting on a new horse for subprocess execution. I believe this PR can be closed and/or reposted in rejuvenated form.

@mih mih closed this Nov 23, 2023
@christian-monch christian-monch deleted the minimal-batch branch July 16, 2024 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants