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

types.jl: sleep with Date.Period types #19749

Merged
merged 1 commit into from
Jan 3, 2017
Merged

Conversation

Shade5
Copy link
Contributor

@Shade5 Shade5 commented Dec 29, 2016

Supports sleep(Dates.Second(10))
sleep(Dates.Milliesecond(10))

Closes #19736

Copy link
Contributor

@TotalVerb TotalVerb left a 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.

@Shade5
Copy link
Contributor Author

Shade5 commented Dec 29, 2016

@TotalVerb I'll make it more generic as you suggested and also include timer and timedwait.
I tried defining these new functions near the old ones but the problem is that while building julia the Date module is not defined yet while the time modules are being built. Thus I had to move it to the Date module

@TotalVerb
Copy link
Contributor

You could consider moving the loading of Dates in sysimg.jl, line 357, a little earlier. Is there a reason why Dates is loaded so late? cc @quinnj

@Shade5
Copy link
Contributor Author

Shade5 commented Dec 29, 2016

@TotalVerb I've added the changes you had suggested

@TotalVerb
Copy link
Contributor

TotalVerb commented Dec 29, 2016

Note that the repeat argument for Timer is also a time period.

@Shade5
Copy link
Contributor Author

Shade5 commented Dec 29, 2016

So, I'll have to add three more functions

Timer(time::Dates.Period, repeat::Real=0.0)
Timer(time::Real, repeat::Dates.Period=0.0)
Timer(time::Dates.Period, repeat::Dates.Period=0.0)

?

@TotalVerb
Copy link
Contributor

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 Dates.Period.

@Shade5
Copy link
Contributor Author

Shade5 commented Dec 29, 2016

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)
Copy link
Contributor

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

Copy link
Contributor Author

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)
Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed!

@kshyatt kshyatt added the dates Dates, times, and the Dates stdlib module label Dec 29, 2016
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)
Copy link
Contributor

@TotalVerb TotalVerb Dec 29, 2016

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

@TotalVerb
Copy link
Contributor

TotalVerb commented Dec 29, 2016

Please add some tests that these methods work properly. See test/channels.jl for a good way to test timedwait; you could repeat the existing test with a Period as argument instead. sleep and Timer can probably be sufficiently tested just by adding some lines to call them and making sure the methods are called properly.

@Shade5
Copy link
Contributor Author

Shade5 commented Dec 30, 2016

@TotalVerb Where are the default tests for sleep and Timer? I've tried, but I can't find them.

@TotalVerb
Copy link
Contributor

There are no tests specifically for sleep and Timer, but it should be enough to test the sleep(Dates.Millisecond(60)) version in threads.jl, replacing the existing sleep(0.06) calls.

@Shade5
Copy link
Contributor Author

Shade5 commented Dec 30, 2016

Alright, I'll add the tests, there seems to be a problem with timedwait. I've posted it on gitter.

@Shade5
Copy link
Contributor Author

Shade5 commented Jan 1, 2017

Added tests

@TotalVerb
Copy link
Contributor

It would be better to avoid duplicating test code. Consider instead a for loop;

for period in (0.06, Dates.Millisecond(60))
    ... # testing code goes here, using `Timer(period)` / `sleep(period)`
end

@Shade5
Copy link
Contributor Author

Shade5 commented Jan 1, 2017

I didn't add the test for timedwait in a forloop as the log output was different

@TotalVerb
Copy link
Contributor

TotalVerb commented Jan 1, 2017

In that case, throw out the original test and keep only the new one. The Period version dispatches to the Real version anyway.

@Shade5
Copy link
Contributor Author

Shade5 commented Jan 1, 2017

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))")
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@TotalVerb
Copy link
Contributor

@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
@Shade5
Copy link
Contributor Author

Shade5 commented Jan 2, 2017

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))
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@TotalVerb
Copy link
Contributor

Travis failure is #19792.

@Shade5
Copy link
Contributor Author

Shade5 commented Jan 2, 2017

@TotalVerb Should I do anything?

@StefanKarpinski StefanKarpinski merged commit ea73c84 into JuliaLang:master Jan 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dates Dates, times, and the Dates stdlib module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants