-
Notifications
You must be signed in to change notification settings - Fork 189
system: subprocessing interface #911
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
Merged
Merged
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
979ba84
add c source
perazz 79ddfc4
add subprocess module
perazz adacbcf
`to_c_string`: move to strings, document
perazz 5b543a2
use temporary `getfile` and `linalg_state_type` f
perazz 519d53d
implement `join`
perazz 1449b8d
fixes to build
perazz cf35194
create submodule
perazz e8451b2
unify `sleep` interface
perazz 48da380
add single-command `run` API
perazz 1f4de32
add tests
perazz 6fbc2e6
getfile: remove trailing new line characters
perazz f9bf304
fix tests to be cross-platform
perazz 71facb3
use `nanosleep` rather than `usleep`
perazz 6ea72d1
add examples
perazz e35b37a
`kill` process
perazz 237e9ff
add process killing example
perazz 2c58fca
on Windows, redirect to `NUL` if output not requested
perazz 136b5b8
remove unused process handle
perazz d8df028
document `run` interface
perazz 94f2bdf
document `is_running`, `is_completed`, `elapsed`
perazz 3fb88e4
add `system` page
perazz 53fc8e5
document `wait`
perazz b30cae4
document `update`
perazz 122fbc6
document `kill`
perazz 56ed7c8
document `sleep`
perazz c617048
document `has_win32`
perazz ed0565c
fix
perazz c03655a
Merge branch 'subprocess' of github.com:perazz/stdlib into subprocess
perazz eb77455
Merge branch 'fortran-lang:master' into subprocess
perazz 74b6ebe
change syntax for `ifx` fix
perazz 9873bc9
fix `sleep` us -> ns
perazz 34732ff
fix `pid` size
perazz 53b03b0
full-cmd: do not use stack
perazz 5a1bd54
fix `sleep`
perazz 9b74bea
process example 2: set max_wait_time
perazz bdb2840
sleep: fix `bind(C)` interface
perazz a1aaf2f
split `run` vs `runasync`
perazz 4d5eb32
`run/runasync` docs
perazz 56f02ab
`has_win32` -> `is_windows`
perazz 15689bc
Update example_process_1.f90
perazz 060dec7
Merge branch 'master' into subprocess
perazz 3560a6f
missing `is_windows` tests
perazz e75bbc9
Merge branch 'subprocess' of github.com:perazz/stdlib into subprocess
perazz d1a4715
Update example_process_4.f90
perazz 68dca8d
Merge branch 'fortran-lang:master' into subprocess
perazz 7653cc4
add object oriented interface
perazz 06c7136
add oop example
perazz f40a547
process ID (`pid`) getter interface
perazz d2ee2f2
implement callback
perazz 3f08a8b
add callback example
perazz d694dcf
fix submodule
perazz 80a2d0a
intel fix: no inline type
perazz 20c045d
document callback and payload functionality
perazz bb98188
Merge branch 'master' into subprocess
perazz 33f81a3
`to_c_string` -> `to_c_char`
perazz d8f8be7
Merge branch 'subprocess' of https://github.com/perazz/stdlib into su…
perazz deabd0c
Update doc/specs/stdlib_system.md
perazz f55ddb7
Update doc/specs/stdlib_system.md
perazz d4422cf
move all examples to separate files
perazz 0675b8c
Merge branch 'subprocess' of https://github.com/perazz/stdlib into su…
perazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
--- | ||
title: system | ||
--- | ||
|
||
# System and sub-processing module | ||
|
||
The `stdlib_system` module provides interface for interacting with external processes, enabling the execution | ||
and monitoring of system commands or applications directly from Fortran. | ||
|
||
[TOC] | ||
|
||
## `run` - Execute an external process synchronously | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `run` interface allows execution of external processes using a single command string or a list of arguments. | ||
Processes run synchronously, meaning execution is blocked until the process finishes. | ||
Optional arguments enable the collection of standard output and error streams, as well as sending input via standard input. | ||
Additionally, a callback function can be specified to execute upon process completion, optionally receiving a user-defined payload. | ||
|
||
### Syntax | ||
|
||
`process = ` [[stdlib_subprocess(module):run(interface)]] `(args [, stdin] [, want_stdout] [, want_stderr] [, callback] [, payload])` | ||
|
||
### Arguments | ||
|
||
`args`: Shall be a `character(*)` string (for command-line execution) or a `character(*), dimension(:)` array (for argument-based execution). It specifies the command and arguments to execute. This is an `intent(in)` argument. | ||
|
||
`stdin` (optional): Shall be a `character(*)` value containing input to send to the process via standard input (pipe). This is an `intent(in)` argument. | ||
|
||
`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. If `.false.` (default), it will be lost. This is an `intent(in)` argument. | ||
|
||
`callback` (optional): Shall be a procedure conforming to the `process_callback` interface. If present, this function will be called upon process completion with the process ID, exit state, and optionally collected standard input, output, and error streams. This is an `intent(in)` argument. | ||
|
||
`payload` (optional): Shall be a generic (`class(*)`) scalar that will be passed to the callback function upon process completion. It allows users to associate custom data with the process execution. This is an `intent(inout), target` argument. | ||
|
||
### Return Value | ||
|
||
Returns an object of type `process_type` that contains information about the state of the created process. | ||
|
||
### Example | ||
|
||
```fortran | ||
! Example usage with command line or list of arguments | ||
type(process_type) :: p | ||
|
||
! Run a simple command line synchronously | ||
p = run("echo 'Hello, world!'", want_stdout=.true.) | ||
``` | ||
|
||
## `runasync` - Execute an external process asynchronously | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `runasync` interface allows execution of external processes using a single command string or a list of arguments. | ||
Processes are run asynchronously (non-blocking), meaning execution does not wait for the process to finish. | ||
Optional arguments enable the collection of standard output and error streams, as well as sending input via standard input. | ||
Additionally, a callback function can be specified to execute upon process completion, optionally receiving a user-defined payload. | ||
|
||
### Syntax | ||
|
||
`process = ` [[stdlib_subprocess(module):runasync(interface)]] `(args [, stdin] [, want_stdout] [, want_stderr] [, callback] [, payload])` | ||
|
||
### Arguments | ||
|
||
`args`: Shall be a `character(*)` string (for command-line execution) or a `character(*), dimension(:)` array (for argument-based execution). It specifies the command and arguments to execute. This is an `intent(in)` argument. | ||
|
||
`stdin` (optional): Shall be a `character(*)` value containing input to send to the process via standard input (pipe). This is an `intent(in)` argument. | ||
|
||
`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. | ||
|
||
`callback` (optional): Shall be a procedure conforming to the `process_callback` interface. If present, this function will be called upon process completion with the process ID, exit state, and optionally collected standard input, output, and error streams. This is an `intent(in)` argument. | ||
|
||
`payload` (optional): Shall be a generic (`class(*)`) scalar that will be passed to the callback function upon process completion. It allows users to associate custom data with the process execution. This is an `intent(inout), target` argument. | ||
|
||
### Return Value | ||
|
||
Returns an object of type `process_type` that contains information about the state of the created process. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_1.f90!} | ||
``` | ||
|
||
## `is_running` - Check if a process is still running | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `is_running` interface provides a method to check if an external process is still running. | ||
This is useful for monitoring the status of asynchronous processes created with the `run` interface. | ||
|
||
### Syntax | ||
|
||
`status = ` [[stdlib_subprocess(module):is_running(interface)]] `(process)` | ||
|
||
### Arguments | ||
|
||
`process`: Shall be a `type(process_type)` object representing the external process to check. This is an `intent(inout)` argument. | ||
|
||
|
||
### Return Value | ||
|
||
Returns a `logical` value: `.true.` if the process is still running, or `.false.` if the process has terminated. | ||
After a call to `is_running`, the `type(process_type)` structure is also updated to the latest process state. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_2.f90!} | ||
``` | ||
|
||
## `is_completed` - Check if a process has completed execution | ||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `is_completed` interface provides a method to check if an external process has finished execution. | ||
This is useful for determining whether asynchronous processes created with the `run` interface have terminated. | ||
|
||
### Syntax | ||
|
||
`status = ` [[stdlib_subprocess(module):is_completed(interface)]] `(process)` | ||
|
||
### Arguments | ||
|
||
`process`: Shall be a `type(process_type)` object representing the external process to check. This is an `intent(inout)` argument. | ||
|
||
### Return Value | ||
|
||
Returns a `logical` value: | ||
- `.true.` if the process has completed. | ||
- `.false.` if the process is still running. | ||
|
||
After a call to `is_completed`, the `type(process_type)` structure is updated to reflect the latest process state. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_1.f90!} | ||
``` | ||
|
||
## `elapsed` - Return process lifetime in seconds | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `elapsed` interface provides a method to calculate the total time that has elapsed since a process was started. | ||
This is useful for tracking the duration of an external process or for performance monitoring purposes. | ||
|
||
The result is a real value representing the elapsed time in seconds, measured from the time the process was created. | ||
|
||
### Syntax | ||
|
||
`delta_t = ` [[stdlib_subprocess(module):elapsed(subroutine)]] `(process)` | ||
|
||
### Arguments | ||
|
||
`process`: Shall be a `type(process_type)` object representing the external process. It is an `intent(in)` argument. | ||
|
||
### Return Value | ||
|
||
Returns a `real(real64)` value that represents the elapsed time (in seconds) since the process was started. | ||
If the process is still running, the value returned is the time elapsed until the call to this function. | ||
Otherwise, the total process duration from creation until completion is returned. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_3.f90!} | ||
``` | ||
|
||
## `wait` - Wait until a running process is completed | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `wait` interface provides a method to block the calling program until the specified process completes. | ||
If the process is running asynchronously, this subroutine will pause the workflow until the given process finishes. | ||
Additionally, an optional maximum wait time can be provided. If the process does not finish within the specified time, | ||
the subroutine will return without waiting further. | ||
|
||
On return from this routine, the process state is accordingly updated. | ||
This is useful when you want to wait for a background task to complete, but want to avoid indefinite blocking | ||
in case of process hang or delay. | ||
|
||
|
||
### Syntax | ||
|
||
`call ` [[stdlib_subprocess(module):wait(subroutine)]] `(process [, max_wait_time])` | ||
|
||
### Arguments | ||
|
||
`process`: Shall be a `type(process_type)` object representing the external process to monitor. | ||
This is an `intent(inout)` argument, and its state is updated upon completion. | ||
|
||
`max_wait_time` (optional): Shall be a `real` value specifying the maximum wait time in seconds. | ||
If not provided, the subroutine will wait indefinitely until the process completes. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_2.f90!} | ||
``` | ||
|
||
## `update` - Update the internal state of a process | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `update` interface allows the internal state of a process object to be updated by querying the system. | ||
After the process completes, the standard output and standard error are retrieved, if they were requested, and loaded into the `process%stdout` and `process%stderr` string variables, respectively. | ||
|
||
This is especially useful for monitoring asynchronous processes and retrieving their output after they have finished. | ||
|
||
### Syntax | ||
|
||
`call ` [[stdlib_subprocess(module):update(subroutine)]] `(process)` | ||
|
||
### Arguments | ||
|
||
`process`: Shall be a `type(process_type)` object representing the external process whose state needs to be updated. | ||
This is an `intent(inout)` argument, and its internal state is updated on completion. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_5.f90!} | ||
``` | ||
|
||
## `kill` - Terminate a running process | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `kill` interface is used to terminate a running external process. It attempts to stop the process and returns a boolean flag indicating whether the operation was successful. | ||
This interface is useful when a process needs to be forcefully stopped, for example, if it becomes unresponsive or if its execution is no longer required. | ||
|
||
### Syntax | ||
|
||
`call ` [[stdlib_subprocess(module):kill(subroutine)]] `(process, success)` | ||
|
||
### Arguments | ||
|
||
`process`: Shall be a `type(process_type)` object representing the external process to be terminated. | ||
This is an `intent(inout)` argument, and on return is updated with the terminated process state. | ||
|
||
`success`: Shall be a `logical` variable. It is set to `.true.` if the process was successfully killed, or `.false.` otherwise. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_4.f90!} | ||
``` | ||
|
||
## `sleep` - Pause execution for a specified time in milliseconds | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `sleep` interface pauses the execution of a program for a specified duration, given in milliseconds. | ||
This routine acts as a cross-platform wrapper, abstracting the underlying platform-specific sleep implementations. | ||
It ensures that the requested sleep duration is honored on both Windows and Unix-like systems. | ||
|
||
### Syntax | ||
|
||
`call ` [[stdlib_system(module):sleep(subroutine)]] `(millisec)` | ||
|
||
### Arguments | ||
|
||
`millisec`: Shall be an `integer` representing the number of milliseconds to sleep. This is an `intent(in)` argument. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_sleep.f90!} | ||
``` | ||
|
||
## `is_windows` - Check if the system is running on Windows | ||
|
||
### Status | ||
|
||
Experimental | ||
|
||
### Description | ||
|
||
The `is_windows` interface provides a quick, compile-time check to determine if the current system is Windows. | ||
It leverages a C function that checks for the presence of the `_WIN32` macro, which is defined in C compilers when targeting Windows. | ||
This function is highly efficient and works during the compilation phase, avoiding the need for runtime checks. | ||
|
||
### Syntax | ||
|
||
`result = ` [[stdlib_system(module):is_windows(function)]] `()` | ||
|
||
### Return Value | ||
|
||
Returns a `logical` flag: `.true.` if the system is Windows, or `.false.` otherwise. | ||
|
||
### Example | ||
|
||
```fortran | ||
{!example/system/example_process_1.f90!} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
ADD_EXAMPLE(process_1) | ||
ADD_EXAMPLE(process_2) | ||
ADD_EXAMPLE(process_3) | ||
ADD_EXAMPLE(process_4) | ||
ADD_EXAMPLE(process_5) | ||
ADD_EXAMPLE(process_6) | ||
ADD_EXAMPLE(process_7) | ||
ADD_EXAMPLE(sleep) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.