Skip to content

Commit 53fc8e5

Browse files
committed
document wait
1 parent 3fb88e4 commit 53fc8e5

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

doc/specs/stdlib_system.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ The result is a real value representing the elapsed time in seconds, measured fr
161161

162162
### Syntax
163163

164-
`delta_t = ` [[stdlib_subprocess(module):elapsed(interface)]] `(process)`
164+
`delta_t = ` [[stdlib_subprocess(module):elapsed(subroutine)]] `(process)`
165165

166166
### Arguments
167167

@@ -189,4 +189,46 @@ delta_t = elapsed(p)
189189
print *, "Elapsed time (s): ", delta_t
190190
```
191191

192+
## `wait` - Wait until a running process is completed
192193

194+
### Status
195+
196+
Experimental
197+
198+
### Description
199+
200+
The `wait` interface provides a method to block the calling program until the specified process completes.
201+
If the process is running asynchronously, this subroutine will pause the workflow until the given process finishes.
202+
Additionally, an optional maximum wait time can be provided. If the process does not finish within the specified time,
203+
the subroutine will return without waiting further.
204+
205+
On return from this routine, the process state is accordingly updated.
206+
This is useful when you want to wait for a background task to complete, but want to avoid indefinite blocking
207+
in case of process hang or delay.
208+
209+
210+
### Syntax
211+
212+
`call ` [[stdlib_subprocess(module):wait(subroutine)]] `(process [, max_wait_time])`
213+
214+
### Arguments
215+
216+
`process`: Shall be a `type(process_type)` object representing the external process to monitor.
217+
This is an `intent(inout)` argument, and its state is updated upon completion.
218+
219+
`max_wait_time` (optional): Shall be a `real` value specifying the maximum wait time in seconds.
220+
If not provided, the subroutine will wait indefinitely until the process completes.
221+
222+
### Example
223+
224+
```fortran
225+
! Example usage of wait
226+
type(process_type) :: p
227+
228+
! Start an asynchronous process
229+
p = run("sleep 5", wait=.false.)
230+
231+
! Wait for process to complete with a 10-second timeout
232+
call wait(p, max_wait_time=10.0)
233+
print *, "Process completed or timed out."
234+
```

src/stdlib_system.F90

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,33 @@ end function process_lifetime
177177
end interface elapsed
178178

179179

180-
!> Wait until a running process is completed
181180
interface wait
181+
!! version: experimental
182+
!!
183+
!! Waits for a running process to complete.
184+
!! ([Specification](../page/specs/stdlib_system.html#wait-wait-until-a-running-process-is-completed))
185+
!!
186+
!! ### Summary
187+
!! Provides a method to block the execution and wait until the specified process finishes.
188+
!! Supports an optional maximum wait time, after which the function returns regardless of process completion.
189+
!!
190+
!! ### Description
191+
!!
192+
!! This interface allows waiting for a process to complete. If the process is running asynchronously, this subroutine
193+
!! will block further execution until the process finishes. Optionally, a maximum wait time can be specified; if
194+
!! the process doesn't complete within this time, the subroutine returns without further waiting.
195+
!!
196+
!! @note The process state is accordingly updated on return from this call.
197+
!!
182198
module subroutine wait_for_completion(process, max_wait_time)
199+
!> The process object to monitor.
183200
class(process_type), intent(inout) :: process
184-
! Optional max wait time in seconds
201+
!> Optional maximum wait time in seconds. If not provided, waits indefinitely.
185202
real, optional, intent(in) :: max_wait_time
186203
end subroutine wait_for_completion
187204
end interface wait
188205

206+
189207
!> Query the system to update a process's state
190208
interface update
191209
module subroutine update_process_state(process)

0 commit comments

Comments
 (0)