poll_file
poll_file(path, interval_s::Real, timeout_s::Real) -> (previous::StatStruct, current::StatStruct)
Monitor a file for changes by polling every interval_s
seconds until a change occurs or timeout_s
seconds have elapsed. The interval_s
should be a long period; the default is 5.007 seconds.
Returns a pair of StatStruct
objects (previous, current)
when a change is detected.
To determine when a file was modified, compare mtime(prev) != mtime(current)
to detect notification of changes. However, using watch_file
for this operation is preferred, since it is more reliable and efficient, although in some situations it may not be available.
Examples
"""
poll_file(path, interval_s::Real, timeout_s::Real) -> (previous::StatStruct, current::StatStruct)
Monitor a file for changes by polling every `interval_s` seconds until a change occurs
or `timeout_s` seconds have elapsed. The `interval_s` should be a long period; the
default is 5.007 seconds.
Returns a pair of `StatStruct` objects `(previous, current)` when a change is detected.
To determine when a file was modified, compare `mtime(prev) != mtime(current)` to detect
notification of changes. However, using `watch_file` for this operation is preferred, since
it is more reliable and efficient, although in some situations it may not be available.
"""
function poll_file(path, interval_s::Real, timeout_s::Real) :: Tuple{StatStruct, StatStruct}
# Implementation code goes here
end
Usage Examples:
-
Poll a file every 10 seconds:
julia> previous, current = poll_file("/path/to/file.txt", 10.0, 60.0)
This example monitors the file located at "/path/to/file.txt" for changes every 10 seconds. It will stop polling if a change is detected or if 60 seconds have elapsed.
-
Use the default interval and timeout values:
julia> previous, current = poll_file("/path/to/another_file.txt", 5.007, 30.0)
In this example, the default interval of 5.007 seconds is used, and the poll will stop after 30 seconds if no changes are detected.
-
Compare modification times of the
StatStruct
objects:julia> previous, current = poll_file("/path/to/some_file.txt", 2.5, 15.0) julia> if mtime(previous) != mtime(current) println("File modified!") else println("No changes detected.") end
This example compares the modification times of the
previous
andcurrent
StatStruct
objects to determine if the file has been modified. If the modification times are different, it prints "File modified!"; otherwise, it prints "No changes detected."
Note: It's recommended to use watch_file
instead of poll_file
whenever possible, as watch_file
provides a more reliable and efficient way to monitor file changes.
See Also
abspath, basename, chmod, countlines, cp, ctime, dirname, download, evalfile, expanduser, fdio, filemode, filesize, functionloc, gperm, homedir, include_string, isabspath, isblockdev, ischardev, isdir, isdirpath, isexecutable, isfifo, isfile, islink, ismount, ispath, isreadable, issetgid, issetuid, issticky, iswritable, joinpath, less, lstat, mkdir, mkpath, mktemp, mktempdir, mtime, mv, normpath, operm, poll_fd, poll_file, readall, readcsv, readdir, readdlm, readlines, readlink, realpath, relpath, rm, splitdir, splitdrive, splitext, stat, symlink, tempdir, tempname, touch, truncate, uperm, watch_file, writecsv,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.