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 53 commits into
base: master
Choose a base branch
from

Conversation

perazz
Copy link
Member

@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 (blocking) process:
    process = run(cmd [, stdin=string] [, want_stdout=.false.] [, want_stderr=.false.])
  • runasync an asynchronous (non-blocking) process:
    process = runasync(cmd [, 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
@perazz
Copy link
Member Author

perazz commented Jan 12, 2025

Summary of community feedback:

  • Have an object-oriented interface in process_type
  • Synchronous/Asynchronous run should be accomplished with different function names rather than the optional wait=.true. input variable
  • Synchronous/Asynchronous interface: proposed names are either run vs. runasync or run vs start
  • Provide an optional callback function interface to be called on project completion
  • have a process ID getter interface
  • document the additional functionality

doc/specs/stdlib_system.md Outdated Show resolved Hide resolved
example/system/example_process_1.f90 Outdated Show resolved Hide resolved
@perazz
Copy link
Member Author

perazz commented Feb 4, 2025

I believe all community-requested features have been introduced.

Copy link
Member

@jvdp1 jvdp1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR @perazz . Very valuable additions.
It could be helpful if the changes in stdlib_strings would have its own PR. It would be easier to review, and I believe it could be merged sooner. But it is also ok like this for me.

@@ -459,8 +459,52 @@ The result is of the same type as `string`.
{!example/strings/example_zfill.f90!}
```

<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
### `join`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have its own PR, as it is not directly associated with this PR (but needed for this PR).

#### Description

Joins an array of strings into a single string. This function concatenates the strings from the input array,
inserting a separator between each string (default: space). A user-defined separator may be provided, The resulting string is returned.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inserting a separator between each string (default: space). A user-defined separator may be provided, The resulting string is returned.
inserting a separator between each string (default: space). A user-defined separator may be provided. The resulting string is returned.

Comment on lines +498 to +503
program test_join
type(string_type) :: result
type(string_type), dimension(3) :: words = [string_type('hello'), string_type('world'), string_type('fortran')]
result = join_string(words, ', ') ! Joins with comma and space
print *, result ! Output: "hello, world, fortran"
end program test_join
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be in a separate file.


`want_stdout` (optional): Shall be a `logical` flag. If `.true.`, the standard output of the process will be captured; if `.false.` (default), it will be lost. This is an `intent(in)` argument.

`want_stderr` (optional): Shall be a logical flag. If `.true.`, the standard error output of the process will be captured. Default: `.false.`. This is an `intent(in)` argument.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`want_stderr` (optional): Shall be a logical flag. If `.true.`, the standard error output of the process will be captured. Default: `.false.`. This is an `intent(in)` argument.
`want_stderr` (optional): Shall be a `logical` flag. If `.true.`, the standard error output of the process will be captured. If `.false.` (default), it will be lost. This is an `intent(in)` argument.


```fortran
! Example usage with command line or list of arguments
type(process_type) :: p
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this could be easily converted to a full program


`want_stdout` (optional): Shall be a `logical` flag. If `.true.`, the standard output of the process will be captured; if `.false.` (default), it will be lost. This is an `intent(in)` argument.

`want_stderr` (optional): Shall be a logical flag. If `.true.`, the standard error output of the process will be captured. Default: `.false.`. This is an `intent(in)` argument.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`want_stderr` (optional): Shall be a logical flag. If `.true.`, the standard error output of the process will be captured. Default: `.false.`. This is an `intent(in)` argument.
`want_stderr` (optional): Shall be a `logical` flag. If `.true.`, the standard error output of the process will be captured. Default: `.false.`. This is an `intent(in)` argument.

end if
```

## `is_completed` - Check if a process has completed execution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the addition of is_completed in comparison to is_running? It seems like the result will always be is_completed(process) == .not.is_running(process)?

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.

4 participants