-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
types.jl: sleep with Date.Period types #19749
Conversation
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 the contribution! A few comments:
As is, this will only work for seconds and milliseconds. That's what I asked for, but it can be made more generic. There's a Dates.toms
function that takes any fixed-length Dates.Period
and gives the number of milliseconds it represents. I would therefore suggest writing
sleep(time::Dates.Period) = sleep(Dates.toms(time) / 1000)
Also, in addition to sleep
, there are functions like Timer
, timedwait
that could receive the same treatment.
I also think that this is not the right file to define these methods in. Could these methods be moved near the other methods for these functions?
As for tests, sleep
is not easy to test properly. But I think having a test that the call does not raise an error is useful. One could do
@test sleep(Dates.Millisecond(10)) === nothing
Finally, make sure the file ends in a new line character. If it doesn't, it's likely that Travis will reject the PR.
@TotalVerb I'll make it more generic as you suggested and also include timer and timedwait. |
You could consider moving the loading of |
@TotalVerb I've added the changes you had suggested |
Note that the |
So, I'll have to add three more functions
? |
I think it would be very confusing to mix real numbers and quantities with units. I would just define a single method for both arguments being |
Done! |
@@ -241,3 +241,8 @@ Base.isless(x::Date,y::Date) = isless(value(x),value(y)) | |||
Base.isless(x::DateTime,y::DateTime) = isless(value(x),value(y)) | |||
Base.isless(x::TimeType,y::TimeType) = isless(promote(x,y)...) | |||
==(x::TimeType,y::TimeType) = ===(promote(x,y)...) | |||
|
|||
import Base: sleep,Timer,timedwait | |||
sleep(time::Dates.Period) = sleep(Dates.toms(time) / 1000) |
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.
don't need Dates.
while inside the Dates
module
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 bad, Fixed it
|
||
import Base: sleep,Timer,timedwait | ||
sleep(time::Period) = sleep(toms(time) / 1000) | ||
Timer(time::Period, repeat::Period=0.0) = Timer(toms(time) / 1000,toms(repeat) / 1000) |
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.
I don't think 0.0
will convert properly to Period
. Use Second(0)
as the default instead.
import Base: sleep,Timer,timedwait | ||
sleep(time::Period) = sleep(toms(time) / 1000) | ||
Timer(time::Period, repeat::Period=0.0) = Timer(toms(time) / 1000,toms(repeat) / 1000) | ||
timedwait(testcb::Function, time::Period, pollint::Float64) = timedwait(testcb, toms(time) / 1000, pollint) |
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.
pollint
is supposed to be a keyword argument; it's also a time.
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.
Changed!
import Base: sleep,Timer,timedwait | ||
sleep(time::Period) = sleep(toms(time) / 1000) | ||
Timer(time::Period, repeat::Period=Second(0)) = Timer(toms(time) / 1000,toms(repeat) / 1000) | ||
timedwait(testcb::Function, time::Period, pollint::Period) = timedwait(testcb, toms(time) / 1000, toms(pollint) / 1000) |
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.
The signature is still not correct. Keyword arguments should be delimited like
timedwait(testcb::Function, time::Period; pollint::Period=Millisecond(100)) = ...
Try to match the existing signature,
function timedwait(testcb::Function, secs::Float64; pollint::Float64=0.1)
...
end
Please add some tests that these methods work properly. See |
@TotalVerb Where are the default tests for sleep and Timer? I've tried, but I can't find them. |
There are no tests specifically for |
Alright, I'll add the tests, there seems to be a problem with timedwait. I've posted it on gitter. |
Added tests |
It would be better to avoid duplicating test code. Consider instead a for period in (0.06, Dates.Millisecond(60))
... # testing code goes here, using `Timer(period)` / `sleep(period)`
end |
I didn't add the test for timedwait in a forloop as the log output was different |
In that case, throw out the original test and keep only the new one. The |
Anything else? |
et=toq() | ||
# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU | ||
try | ||
@assert (et >= 1.0) && (et <= 1.5) | ||
@assert !isready(rr3) | ||
catch | ||
warn("timedwait tests delayed. et=$et, isready(rr3)=$(isready(rr3))") | ||
warn("timedwait-Period tests delayed. et=$et, isready(rr3)=$(isready(rr3))") |
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.
No need for this -Period
here.
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.
👍
@tkelman I suppose since it is now past feature freeze, this will have to wait until 1.0, right? |
Supports sleep(Dates.Second(10)) sleep(Dates.Milliesecond(10)) Closes JuliaLang#19736
Why is travis failing? |
@@ -92,7 +92,7 @@ end | |||
@async begin sleep(2.0); put!(rr3, :ok) end | |||
|
|||
tic() | |||
timedwait(callback, 1.0) | |||
timedwait(callback, Dates.Second(1)) |
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.
why is this necessary, shouldn't timedwait(callback, 1.0)
still work?
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.
As TotalVerb said, The Period version dispatches to the Real version anyway.
Travis failure is #19792. |
@TotalVerb Should I do anything? |
Supports sleep(Dates.Second(10))
sleep(Dates.Milliesecond(10))
Closes #19736