-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Task.sleep(for: Duration)
#59203
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
Conversation
public static func sleep( | ||
for duration: Duration | ||
) async throws { | ||
try await sleep(until: .now + duration, clock: .continuous) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me from the docs when one would reach for a suspending clock, so I went with .continuous
, but I may just not understand their uses well enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so suspending will mean that if the machine is asleep then the clock makes no forward progress so for example
sleep for 5 seconds
3 seconds later the machine is suspended
10 seconds later the machine is resumed
sleep continues for 2 seconds
for the continuous clock it would work as such:
sleep for 5 seconds
3 seconds later the machine is suspended
10 seconds later the machine is resumed
sleep resumes because 5 seconds has elapsed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my guess is that more often than not, this particular variant would be the latter (continuous as you had a intuition for). If developers want the alternate version then they can use the slightly more verbose one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so suspending will mean that if the machine is asleep then the clock makes no forward progress so for example
That was my understanding, too, I just can't think through when a suspending clock would be preferred :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phausler You said Continuous in the review thread: https://forums.swift.org/t/se-0329-second-review-clock-instant-and-duration/54509/3 . Is it possible to clarify this in the text of SE-0329?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this looks correct to me
@swift-ci please smoke test |
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this and for filling in documentation comments. The docs mostly look good, but left a couple minor changes.
Co-authored-by: Alex Martini <amartini@apple.com>
Co-authored-by: Alex Martini <amartini@apple.com>
@swift-ci Please smoke test. |
Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
@swift-ci Please smoke test |
Is anything in particular blocking this? Or did it get lost in the WWDC shuffle? 😅 |
Seems everyone approved and all that's waiting is for it to be merged... |
* Add Task.sleep(for: Duration) * Update TaskSleepDuration.swift * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Alex Martini <amartini@apple.com> * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Alex Martini <amartini@apple.com> * Fix suggestion. * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> * Update stdlib/public/Concurrency/TaskSleepDuration.swift Co-authored-by: Alex Martini <amartini@apple.com> Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
This PR introduces missing functionality from SE-0329, namely the
Task.sleep
helper that takes aDuration
.I PRed against
main
but this should also be pulled into 5.7./cc @phausler