Skip to content

Commit 20c045d

Browse files
committed
document callback and payload functionality
1 parent 80a2d0a commit 20c045d

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

doc/specs/stdlib_system.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ Experimental
1919

2020
The `run` interface allows execution of external processes using a single command string or a list of arguments.
2121
Processes run synchronously, meaning execution is blocked until the process finishes.
22-
Optional arguments enable the collection of standard output and error streams, as well as sending input via standard input.
22+
Optional arguments enable the collection of standard output and error streams, as well as sending input via standard input.
23+
Additionally, a callback function can be specified to execute upon process completion, optionally receiving a user-defined payload.
2324

2425
### Syntax
2526

26-
`process = ` [[stdlib_subprocess(module):run(interface)]] `(args [, stdin] [, want_stdout] [, want_stderr])`
27+
`process = ` [[stdlib_subprocess(module):run(interface)]] `(args [, stdin] [, want_stdout] [, want_stderr] [, callback] [, payload])`
2728

2829
### Arguments
2930

@@ -35,6 +36,10 @@ Optional arguments enable the collection of standard output and error streams, a
3536

3637
`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.
3738

39+
`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.
40+
41+
`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.
42+
3843
### Return Value
3944

4045
Returns an object of type `process_type` that contains information about the state of the created process.
@@ -49,7 +54,6 @@ type(process_type) :: p
4954
p = run("echo 'Hello, world!'", want_stdout=.true.)
5055
```
5156

52-
5357
## `runasync` - Execute an external process asynchronously
5458

5559
### Status
@@ -61,10 +65,11 @@ Experimental
6165
The `runasync` interface allows execution of external processes using a single command string or a list of arguments.
6266
Processes are run asynchronously (non-blocking), meaning execution does not wait for the process to finish.
6367
Optional arguments enable the collection of standard output and error streams, as well as sending input via standard input.
68+
Additionally, a callback function can be specified to execute upon process completion, optionally receiving a user-defined payload.
6469

6570
### Syntax
6671

67-
`process = ` [[stdlib_subprocess(module):runasync(interface)]] `(args [, stdin] [, want_stdout] [, want_stderr])`
72+
`process = ` [[stdlib_subprocess(module):runasync(interface)]] `(args [, stdin] [, want_stdout] [, want_stderr] [, callback] [, payload])`
6873

6974
### Arguments
7075

@@ -76,6 +81,10 @@ Optional arguments enable the collection of standard output and error streams, a
7681

7782
`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.
7883

84+
`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.
85+
86+
`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.
87+
7988
### Return Value
8089

8190
Returns an object of type `process_type` that contains information about the state of the created process.

src/stdlib_system.F90

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module stdlib_system
8585
!! version: experimental
8686
!!
8787
!! Executes an external process asynchronously.
88-
!! ([Specification](../page/specs/stdlib_system.html#run-execute-an-external-process-asynchronously))
88+
!! ([Specification](../page/specs/stdlib_system.html#runasync-execute-an-external-process-asynchronously))
8989
!!
9090
!! ### Summary
9191
!! Provides methods for executing external processes asynchronously, using either a single command string
@@ -96,8 +96,10 @@ module stdlib_system
9696
!! This interface allows the user to spawn external processes asynchronously (non-blocking).
9797
!! Processes can be executed via a single command string or a list of arguments, with options to collect
9898
!! standard output and error streams, or to provide a standard input stream via a `character` string.
99+
!! Additionally, a callback function can be provided, which will be called upon process completion.
100+
!! A user-defined payload can be attached and passed to the callback for handling process-specific data.
99101
!!
100-
!! @note The implementation depends on system-level process management capabilitiesa
102+
!! @note The implementation depends on system-level process management capabilities.
101103
!!
102104
module function run_async_cmd(cmd, stdin, want_stdout, want_stderr, callback, payload) result(process)
103105
!> The command line string to execute.
@@ -139,7 +141,7 @@ end function run_async_args
139141
!! version: experimental
140142
!!
141143
!! Executes an external process synchronously.
142-
!! ([Specification](../page/specs/stdlib_system.html#runasync-execute-an-external-process-synchronously))
144+
!! ([Specification](../page/specs/stdlib_system.html#run-execute-an-external-process-synchronously))
143145
!!
144146
!! ### Summary
145147
!! Provides methods for executing external processes synchronously, using either a single command string
@@ -150,6 +152,9 @@ end function run_async_args
150152
!! This interface allows the user to spawn external processes synchronously (blocking),
151153
!! via either a single command string or a list of arguments. It also includes options to collect
152154
!! standard output and error streams, or to provide a standard input stream via a `character` string.
155+
!! Additionally, it supports an optional callback function that is invoked upon process completion,
156+
!! allowing users to process results dynamically. A user-defined payload can also be provided,
157+
!! which is passed to the callback function to facilitate contextual processing.
153158
!!
154159
!! @note The implementation depends on system-level process management capabilities.
155160
!!
@@ -367,6 +372,18 @@ end subroutine sleep
367372
end interface sleep
368373

369374
abstract interface
375+
376+
!! version: experimental
377+
!!
378+
!! Process callback interface
379+
!!
380+
!! ### Summary
381+
!!
382+
!! The `process_callback` interface defines a user-provided subroutine that will be called
383+
!! upon process completion. It provides access to process metadata, including the process ID,
384+
!! exit state, and optional input/output streams. If passed on creation, a generic payload can be
385+
!! accessed by the callback function. This variable must be a valid `target` in the calling scope.
386+
!!
370387
subroutine process_callback(pid,exit_state,stdin,stdout,stderr,payload)
371388
import process_ID
372389
implicit none

0 commit comments

Comments
 (0)