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

Merged
merged 60 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
979ba84
add c source
perazz Dec 24, 2024
79ddfc4
add subprocess module
perazz Dec 24, 2024
adacbcf
`to_c_string`: move to strings, document
perazz Dec 24, 2024
5b543a2
use temporary `getfile` and `linalg_state_type` f
perazz Dec 24, 2024
519d53d
implement `join`
perazz Dec 24, 2024
1449b8d
fixes to build
perazz Dec 24, 2024
cf35194
create submodule
perazz Dec 24, 2024
e8451b2
unify `sleep` interface
perazz Dec 24, 2024
48da380
add single-command `run` API
perazz Dec 24, 2024
1f4de32
add tests
perazz Dec 24, 2024
6fbc2e6
getfile: remove trailing new line characters
perazz Dec 24, 2024
f9bf304
fix tests to be cross-platform
perazz Dec 24, 2024
71facb3
use `nanosleep` rather than `usleep`
perazz Dec 24, 2024
6ea72d1
add examples
perazz Dec 24, 2024
e35b37a
`kill` process
perazz Dec 24, 2024
237e9ff
add process killing example
perazz Dec 24, 2024
2c58fca
on Windows, redirect to `NUL` if output not requested
perazz Dec 24, 2024
136b5b8
remove unused process handle
perazz Dec 25, 2024
d8df028
document `run` interface
perazz Dec 25, 2024
94f2bdf
document `is_running`, `is_completed`, `elapsed`
perazz Dec 25, 2024
3fb88e4
add `system` page
perazz Dec 25, 2024
53fc8e5
document `wait`
perazz Dec 25, 2024
b30cae4
document `update`
perazz Dec 25, 2024
122fbc6
document `kill`
perazz Dec 25, 2024
56ed7c8
document `sleep`
perazz Dec 25, 2024
c617048
document `has_win32`
perazz Dec 25, 2024
ed0565c
fix
perazz Dec 25, 2024
c03655a
Merge branch 'subprocess' of github.com:perazz/stdlib into subprocess
perazz Dec 26, 2024
eb77455
Merge branch 'fortran-lang:master' into subprocess
perazz Dec 26, 2024
74b6ebe
change syntax for `ifx` fix
perazz Dec 26, 2024
9873bc9
fix `sleep` us -> ns
perazz Dec 26, 2024
34732ff
fix `pid` size
perazz Dec 26, 2024
53b03b0
full-cmd: do not use stack
perazz Dec 26, 2024
5a1bd54
fix `sleep`
perazz Dec 26, 2024
9b74bea
process example 2: set max_wait_time
perazz Dec 26, 2024
bdb2840
sleep: fix `bind(C)` interface
perazz Dec 26, 2024
a1aaf2f
split `run` vs `runasync`
perazz Jan 28, 2025
4d5eb32
`run/runasync` docs
perazz Jan 28, 2025
56f02ab
`has_win32` -> `is_windows`
perazz Jan 28, 2025
15689bc
Update example_process_1.f90
perazz Jan 28, 2025
060dec7
Merge branch 'master' into subprocess
perazz Jan 28, 2025
3560a6f
missing `is_windows` tests
perazz Jan 28, 2025
e75bbc9
Merge branch 'subprocess' of github.com:perazz/stdlib into subprocess
perazz Jan 28, 2025
d1a4715
Update example_process_4.f90
perazz Jan 28, 2025
68dca8d
Merge branch 'fortran-lang:master' into subprocess
perazz Jan 29, 2025
7653cc4
add object oriented interface
perazz Feb 4, 2025
06c7136
add oop example
perazz Feb 4, 2025
f40a547
process ID (`pid`) getter interface
perazz Feb 4, 2025
d2ee2f2
implement callback
perazz Feb 4, 2025
3f08a8b
add callback example
perazz Feb 4, 2025
d694dcf
fix submodule
perazz Feb 4, 2025
80a2d0a
intel fix: no inline type
perazz Feb 4, 2025
20c045d
document callback and payload functionality
perazz Feb 4, 2025
bb98188
Merge branch 'master' into subprocess
perazz Feb 17, 2025
33f81a3
`to_c_string` -> `to_c_char`
perazz Feb 17, 2025
d8f8be7
Merge branch 'subprocess' of https://github.com/perazz/stdlib into su…
perazz Feb 17, 2025
deabd0c
Update doc/specs/stdlib_system.md
perazz Feb 17, 2025
f55ddb7
Update doc/specs/stdlib_system.md
perazz Feb 17, 2025
d4422cf
move all examples to separate files
perazz Feb 17, 2025
0675b8c
Merge branch 'subprocess' of https://github.com/perazz/stdlib into su…
perazz Feb 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
document wait
  • Loading branch information
perazz committed Dec 25, 2024
commit 53fc8e59093b1f3a83a7c53bb5471a03b6ef6940
44 changes: 43 additions & 1 deletion doc/specs/stdlib_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The result is a real value representing the elapsed time in seconds, measured fr

### Syntax

`delta_t = ` [[stdlib_subprocess(module):elapsed(interface)]] `(process)`
`delta_t = ` [[stdlib_subprocess(module):elapsed(subroutine)]] `(process)`

### Arguments

Expand Down Expand Up @@ -189,4 +189,46 @@ delta_t = elapsed(p)
print *, "Elapsed time (s): ", delta_t
```

## `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 usage of wait
type(process_type) :: p

! Start an asynchronous process
p = run("sleep 5", wait=.false.)

! Wait for process to complete with a 10-second timeout
call wait(p, max_wait_time=10.0)
print *, "Process completed or timed out."
```
22 changes: 20 additions & 2 deletions src/stdlib_system.F90
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,33 @@ end function process_lifetime
end interface elapsed


!> Wait until a running process is completed
interface wait
!! version: experimental
!!
!! Waits for a running process to complete.
!! ([Specification](../page/specs/stdlib_system.html#wait-wait-until-a-running-process-is-completed))
!!
!! ### Summary
!! Provides a method to block the execution and wait until the specified process finishes.
!! Supports an optional maximum wait time, after which the function returns regardless of process completion.
!!
!! ### Description
!!
!! This interface allows waiting for a process to complete. If the process is running asynchronously, this subroutine
!! will block further execution until the process finishes. Optionally, a maximum wait time can be specified; if
!! the process doesn't complete within this time, the subroutine returns without further waiting.
!!
!! @note The process state is accordingly updated on return from this call.
!!
module subroutine wait_for_completion(process, max_wait_time)
!> The process object to monitor.
class(process_type), intent(inout) :: process
! Optional max wait time in seconds
!> Optional maximum wait time in seconds. If not provided, waits indefinitely.
real, optional, intent(in) :: max_wait_time
end subroutine wait_for_completion
end interface wait


!> Query the system to update a process's state
interface update
module subroutine update_process_state(process)
Expand Down