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

system: subprocessing interface #911

Open
wants to merge 36 commits into
base: master
Choose a base branch
from

Conversation

perazz
Copy link
Contributor

@perazz perazz commented Dec 26, 2024

With this PR I want to introduce for discussion a comprehensive API for managing external processes in Fortran.
This is a fully cross-platform implementation for synchronous (similar to execute_command_line) or asynchronous (fully detached process) processes, and mimics functionality from the Python subprocess module.

Previous discussion

Proposed API

Here’s the simplified list of interfaces with their types and example usage:

  • type(process_type): A derived type representing the external process state containing the state handler as well as stdin, stdout, stderr as strings.
  • run a synchronous or asynchronous process:
    process = run(cmd [, wait=.true,] [, stdin=string] [, want_stdout=.false.] [, want_stderr=.false.])
  • update query process state and update the state variable:
    call update(process)
  • is_running check if a process is still running and update the handler:
    status = is_running(process)
  • is_completed check if a process has finished and update the handler:
    status = is_completed(process)
  • wait for completion (option for max waiting time in seconds):
    call wait(process [, max_wait_time=10.0])
  • elapsed time since process start, or total process lifetime:
    duration = elapsed(process)
  • kill a running external process:
    call kill(process, success)
  • sleep implementation is moved to C so that a slightly better wrapper based on nanosecond can be used

This provides a concise overview of the interfaces for quick reference.

Key facts

  • Traditional Fortran interfaces are used rather than type-bound procedures because that seems more similar to the other stdlib interfaces, but I'm happy to introduce them as well and further discuss the API.
  • stderr, stdout, stdin are stored via temporary files, but that is completely internal to the implementation. So, we can later implement faster memory-only communication via pipes without changing the API.
  • A cross-platform C interface routine was necessary. Because C macros are more informative, all platform-dependent operation is kept on the C side only.
  • Error handling is currently made via integer exit codes. If/when the type(state_type) general error handler is approved, we can improve this and use it throughout the subprocessing API too.

Prior art

@ivan-pi
Copy link
Member

ivan-pi commented Dec 26, 2024

Cool addition! I always wanted to have something like this in the past. I wrote my own little experiment once. I started reviewing the Python subprocess module, but got discouraged by the messy implementation details.

@perazz perazz marked this pull request as ready for review December 26, 2024 18:27
@perazz perazz mentioned this pull request Jan 3, 2025
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