[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Timer Class

Mohamed Hussain

4/27/2007 7:19:00 AM

Hai Dude

I want to write one class that class should act as a Timer
On that class , i will check the database every second for database
field called Diaplay_time ,if the current system time matched with
database Display_Time field data then i will show the message in
console,That class should terminate only when i press "Ctrl+C" key
otherwise that wont stop
ok
can any body tell me the solution?
I need to do that
ok
Bye
By
P.S.Hussain

--
Posted via http://www.ruby-....

42 Answers

Alex Young

4/27/2007 8:00:00 AM

0

Hussain wrote:
> Hai Dude
>
> I want to write one class that class should act as a Timer
> On that class , i will check the database every second for database
> field called Diaplay_time ,if the current system time matched with
> database Display_Time field data then i will show the message in
> console,That class should terminate only when i press "Ctrl+C" key
> otherwise that wont stop

You can write a very simple while loop for this:

while true do
start_time = Time.now
#do database stuff
total_time = Time.now - start_time
sleep(1.0 - total_time) # ignoring the possibility of total_time > 1
end

Alternatively, to make it more Rubyish, use a block or two (untested):

def time_block
start_time = Time.now
yield
return Time.now - start_time
end

def repeat_every(seconds)
while true do
sleep( seconds - time_block { yield } ) # again ignoring time > seconds
end
end

Use it like this:

repeat_every(1.0) do
# do database stuff
end

--
Alex

Siddharth Karandikar

4/27/2007 9:05:00 AM

0

Hi Alex,

On 4/27/07, Alex Young <alex@blackkettle.org> wrote:
> Hussain wrote:
> > Hai Dude
> >
> > I want to write one class that class should act as a Timer
> > On that class , i will check the database every second for database
> > field called Diaplay_time ,if the current system time matched with
> > database Display_Time field data then i will show the message in
> > console,That class should terminate only when i press "Ctrl+C" key
> > otherwise that wont stop
>
> You can write a very simple while loop for this:
>
> while true do
> start_time = Time.now
> #do database stuff
> total_time = Time.now - start_time
> sleep(1.0 - total_time) # ignoring the possibility of total_time > 1
> end
>
> Alternatively, to make it more Rubyish, use a block or two (untested):
>
> def time_block
> start_time = Time.now
> yield
> return Time.now - start_time
> end
>
> def repeat_every(seconds)
> while true do
> sleep( seconds - time_block { yield } ) # again ignoring time > seconds
> end
> end
>

After seeing this code, I thought, how we can make this reentrant.
Here is what I could come up with.
------
def time_block
start_time = Time.now
Thread.new { yield }
Time.now - start_time
end

def repeat_every(seconds)
while true do
time_spent = time_block { yield } # To handle -ve sleep interaval
sleep(seconds - time_spent) if time_spent < seconds
end
end

repeat_every(1) {
puts "Task started. #{Time.now}"
sleep(2)
puts "Task done. #{Time.now}"
}
------

What precautions should one take in such code involving threads and sleep?


> Use it like this:
>
> repeat_every(1.0) do
> # do database stuff
> end
>
> --
> Alex
>
>


--
-------------------------------------------
-|[Siddharth] [S] [Karandikar]|-
www.paahijen.com - Applications in Indian Languages!

http://www.flickr.com/photos/sid...
-------------------------------------------

Paul Brannan

4/27/2007 1:23:00 PM

0

On Fri, Apr 27, 2007 at 04:19:18PM +0900, Hussain wrote:
> Hai Dude
>
> I want to write one class that class should act as a Timer
> On that class , i will check the database every second for database
> field called Diaplay_time ,if the current system time matched with
> database Display_Time field data then i will show the message in
> console,That class should terminate only when i press "Ctrl+C" key
> otherwise that wont stop
> ok
> can any body tell me the solution?
> I need to do that

In general there are two common approaches:
- use threads
- use an event loop

In Ruby, threads are easy to write. To set up a timer using threads,
create a thread that sleeps for one second, wakes up, then does your
check. The disadvantage to this approach is that your code must be
thread-safe.

With an event loop, your code waits for an event (a timer to fire, input
from the user, etc.) and responds to that event. There are numerous
libraries out there to help with this, including Ruby/Event and
IO::Reactor.

Paul


Ben Cramer#1

6/14/2009 8:55:00 AM

0

I'll Always Be 14/06/09 wrote:
> In article
> <79jgniF1qpjrfU1@mid.individual.net>,
> "Heinrich" <Heinrich@Ruhrgasnet.de>
> wrote:
>
>> If yesterday's Holocaust Museum slaying of security guard and national hero
>> Stephen Tyrone Johns is not a clarion call for banning hate speech, I don't
>> know what is. Playwright Janet Langhart Cohen appeared on CNN yesterday
>> right after the shooting, as she wrote a play that was supposed to have been
>> debuted at the Holocaust Museum last night. Her play is about Emmett Till,
>> whose lynching helped launch the Civil Rights Movement, and Ann Frank, whose
>> diary told the story of Holocaust victims in hiding in the Netherlands
>> during World War II.
>>
>> She said something must be done about ridding the Internet and the public
>> dialogue of hate speech. I agree. Not only have we had three hate crime
>> murders within the last two weeks (Mr. Johns, as noted above, Dr. George
>> Tiller a week ago last Sunday, and Pvt. William Andrew Long by an
>> American-born Muslim convert outside a recruiting station just before that.)
>>
>> Now we have this quote from the so-called Rev. Jeremiah Wright, who used to
>> be President Obama's pastor. Hate comes from among all peoples and all
>> religions. He said this about his lack of communication with Barack Obama
>> since he's been elected president, according to the AP:
>>
>> "Them Jews ain't going to let him talk to me. I told my baby daughter that
>> he'll talk to me in five years when he's a lame duck, or in eight years when
>> he's out of office," Wright told the Daily Press of Newport News following a
>> Tuesday night sermon at the 95th annual Hampton University Ministers'
>> Conference.
>>
>> It's not enough to prosecute these murders as murders. They are
>> hate-motivated crimes and each of these men had been under some sort of
>> police surveillance prior to their actions. Isn't it time we started
>> rounding up promoters of hate before they kill?
>>
>> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
>
> Make sure kurt knoll is first. We've
> already got benji

really?

Alexander

6/14/2009 8:56:00 AM

0

Heinrich wrote:
>
> "I'll Always Be 14/06/09" <aussies_suck@invalid.invalid> schreef in
> bericht news:aussies_suck-72842F.01140314062009@aries.ka.weretis.net...
>> In article
>> <79jgniF1qpjrfU1@mid.individual.net>,
>> "Heinrich" <Heinrich@Ruhrgasnet.de>
>> wrote:
>>
>>> If yesterday's Holocaust Museum slaying of security guard and
>>> national hero
>>> Stephen Tyrone Johns is not a clarion call for banning hate speech, I
>>> don't
>>> know what is. Playwright Janet Langhart Cohen appeared on CNN yesterday
>>> right after the shooting, as she wrote a play that was supposed to
>>> have been
>>> debuted at the Holocaust Museum last night. Her play is about Emmett
>>> Till,
>>> whose lynching helped launch the Civil Rights Movement, and Ann
>>> Frank, whose
>>> diary told the story of Holocaust victims in hiding in the Netherlands
>>> during World War II.
>>>
>>> She said something must be done about ridding the Internet and the
>>> public
>>> dialogue of hate speech. I agree. Not only have we had three hate crime
>>> murders within the last two weeks (Mr. Johns, as noted above, Dr. George
>>> Tiller a week ago last Sunday, and Pvt. William Andrew Long by an
>>> American-born Muslim convert outside a recruiting station just before
>>> that.)
>>>
>>> Now we have this quote from the so-called Rev. Jeremiah Wright, who
>>> used to
>>> be President Obama's pastor. Hate comes from among all peoples and all
>>> religions. He said this about his lack of communication with Barack
>>> Obama
>>> since he's been elected president, according to the AP:
>>>
>>> "Them Jews ain't going to let him talk to me. I told my baby daughter
>>> that
>>> he'll talk to me in five years when he's a lame duck, or in eight
>>> years when
>>> he's out of office," Wright told the Daily Press of Newport News
>>> following a
>>> Tuesday night sermon at the 95th annual Hampton University Ministers'
>>> Conference.
>>>
>>> It's not enough to prosecute these murders as murders. They are
>>> hate-motivated crimes and each of these men had been under some sort of
>>> police surveillance prior to their actions. Isn't it time we started
>>> rounding up promoters of hate before they kill?
>>>
>>> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
>>>
>>
>> Make sure kurt knoll is first. We've
>> already got benji
>
> we? where have you been coward? are you ready for your daily spanking?

He has been very busy visiting his parole officer.

Heinrich

6/14/2009 8:59:00 AM

0


"Alexander" <Alexander@thegreat.org> schreef in bericht
news:79js9aF1qt8dhU3@mid.individual.net...
> Heinrich wrote:
>>
>> "I'll Always Be 14/06/09" <aussies_suck@invalid.invalid> schreef in
>> bericht news:aussies_suck-72842F.01140314062009@aries.ka.weretis.net...
>>> In article
>>> <79jgniF1qpjrfU1@mid.individual.net>,
>>> "Heinrich" <Heinrich@Ruhrgasnet.de>
>>> wrote:
>>>
>>>> If yesterday's Holocaust Museum slaying of security guard and national
>>>> hero
>>>> Stephen Tyrone Johns is not a clarion call for banning hate speech, I
>>>> don't
>>>> know what is. Playwright Janet Langhart Cohen appeared on CNN yesterday
>>>> right after the shooting, as she wrote a play that was supposed to have
>>>> been
>>>> debuted at the Holocaust Museum last night. Her play is about Emmett
>>>> Till,
>>>> whose lynching helped launch the Civil Rights Movement, and Ann Frank,
>>>> whose
>>>> diary told the story of Holocaust victims in hiding in the Netherlands
>>>> during World War II.
>>>>
>>>> She said something must be done about ridding the Internet and the
>>>> public
>>>> dialogue of hate speech. I agree. Not only have we had three hate crime
>>>> murders within the last two weeks (Mr. Johns, as noted above, Dr.
>>>> George
>>>> Tiller a week ago last Sunday, and Pvt. William Andrew Long by an
>>>> American-born Muslim convert outside a recruiting station just before
>>>> that.)
>>>>
>>>> Now we have this quote from the so-called Rev. Jeremiah Wright, who
>>>> used to
>>>> be President Obama's pastor. Hate comes from among all peoples and all
>>>> religions. He said this about his lack of communication with Barack
>>>> Obama
>>>> since he's been elected president, according to the AP:
>>>>
>>>> "Them Jews ain't going to let him talk to me. I told my baby daughter
>>>> that
>>>> he'll talk to me in five years when he's a lame duck, or in eight years
>>>> when
>>>> he's out of office," Wright told the Daily Press of Newport News
>>>> following a
>>>> Tuesday night sermon at the 95th annual Hampton University Ministers'
>>>> Conference.
>>>>
>>>> It's not enough to prosecute these murders as murders. They are
>>>> hate-motivated crimes and each of these men had been under some sort of
>>>> police surveillance prior to their actions. Isn't it time we started
>>>> rounding up promoters of hate before they kill?
>>>>
>>>> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
>>>
>>> Make sure kurt knoll is first. We've
>>> already got benji
>>
>> we? where have you been coward? are you ready for your daily spanking?
>
> He has been very busy visiting his parole officer.

yes, but i miss him already with his daily nonsense

Heinrich

6/14/2009 8:59:00 AM

0


"Ben Cramer#1" <No1@Cloned.com> schreef in bericht
news:79js7eF1qt8dhU2@mid.individual.net...
> I'll Always Be 14/06/09 wrote:
>> In article <79jgniF1qpjrfU1@mid.individual.net>,
>> "Heinrich" <Heinrich@Ruhrgasnet.de> wrote:
>>
>>> If yesterday's Holocaust Museum slaying of security guard and national
>>> hero Stephen Tyrone Johns is not a clarion call for banning hate speech,
>>> I don't know what is. Playwright Janet Langhart Cohen appeared on CNN
>>> yesterday right after the shooting, as she wrote a play that was
>>> supposed to have been debuted at the Holocaust Museum last night. Her
>>> play is about Emmett Till, whose lynching helped launch the Civil Rights
>>> Movement, and Ann Frank, whose diary told the story of Holocaust victims
>>> in hiding in the Netherlands during World War II.
>>>
>>> She said something must be done about ridding the Internet and the
>>> public dialogue of hate speech. I agree. Not only have we had three hate
>>> crime murders within the last two weeks (Mr. Johns, as noted above, Dr.
>>> George Tiller a week ago last Sunday, and Pvt. William Andrew Long by an
>>> American-born Muslim convert outside a recruiting station just before
>>> that.)
>>>
>>> Now we have this quote from the so-called Rev. Jeremiah Wright, who used
>>> to be President Obama's pastor. Hate comes from among all peoples and
>>> all religions. He said this about his lack of communication with Barack
>>> Obama since he's been elected president, according to the AP:
>>>
>>> "Them Jews ain't going to let him talk to me. I told my baby daughter
>>> that he'll talk to me in five years when he's a lame duck, or in eight
>>> years when he's out of office," Wright told the Daily Press of Newport
>>> News following a Tuesday night sermon at the 95th annual Hampton
>>> University Ministers' Conference.
>>>
>>> It's not enough to prosecute these murders as murders. They are
>>> hate-motivated crimes and each of these men had been under some sort of
>>> police surveillance prior to their actions. Isn't it time we started
>>> rounding up promoters of hate before they kill?
>>>
>>> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
>>
>> Make sure kurt knoll is first. We've already got benji
>
> really?
of course not.ben is having a sabbatical and will be back in due course.

I'll Never Be 12/08/11

6/14/2009 9:10:00 AM

0

In article
<79js7eF1qt8dhU2@mid.individual.net>,
Ben Cramer#1 <No1@Cloned.com> wrote:

> I'll Always Be 14/06/09 wrote:
> > In article
> > <79jgniF1qpjrfU1@mid.individual.net>,
> > "Heinrich" <Heinrich@Ruhrgasnet.de>
> > wrote:
> >
> >> If yesterday's Holocaust Museum slaying of security guard and national
> >> hero
> >> Stephen Tyrone Johns is not a clarion call for banning hate speech, I
> >> don't
> >> know what is. Playwright Janet Langhart Cohen appeared on CNN yesterday
> >> right after the shooting, as she wrote a play that was supposed to have
> >> been
> >> debuted at the Holocaust Museum last night. Her play is about Emmett Till,
> >> whose lynching helped launch the Civil Rights Movement, and Ann Frank,
> >> whose
> >> diary told the story of Holocaust victims in hiding in the Netherlands
> >> during World War II.
> >>
> >> She said something must be done about ridding the Internet and the public
> >> dialogue of hate speech. I agree. Not only have we had three hate crime
> >> murders within the last two weeks (Mr. Johns, as noted above, Dr. George
> >> Tiller a week ago last Sunday, and Pvt. William Andrew Long by an
> >> American-born Muslim convert outside a recruiting station just before
> >> that.)
> >>
> >> Now we have this quote from the so-called Rev. Jeremiah Wright, who used
> >> to
> >> be President Obama's pastor. Hate comes from among all peoples and all
> >> religions. He said this about his lack of communication with Barack Obama
> >> since he's been elected president, according to the AP:
> >>
> >> "Them Jews ain't going to let him talk to me. I told my baby daughter that
> >> he'll talk to me in five years when he's a lame duck, or in eight years
> >> when
> >> he's out of office," Wright told the Daily Press of Newport News following
> >> a
> >> Tuesday night sermon at the 95th annual Hampton University Ministers'
> >> Conference.
> >>
> >> It's not enough to prosecute these murders as murders. They are
> >> hate-motivated crimes and each of these men had been under some sort of
> >> police surveillance prior to their actions. Isn't it time we started
> >> rounding up promoters of hate before they kill?
> >>
> >> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
> >>
> >
> > Make sure kurt knoll is first. We've
> > already got benji
>
> really?

Just ask eugene

Ben Cramer#1

6/14/2009 9:49:00 AM

0

I'll Always Be 14/06/09 wrote:
> In article
> <79js7eF1qt8dhU2@mid.individual.net>,
> Ben Cramer#1 <No1@Cloned.com> wrote:
>
>> I'll Always Be 14/06/09 wrote:
>>> In article
>>> <79jgniF1qpjrfU1@mid.individual.net>,
>>> "Heinrich" <Heinrich@Ruhrgasnet.de>
>>> wrote:
>>>
>>>> If yesterday's Holocaust Museum slaying of security guard and national
>>>> hero
>>>> Stephen Tyrone Johns is not a clarion call for banning hate speech, I
>>>> don't
>>>> know what is. Playwright Janet Langhart Cohen appeared on CNN yesterday
>>>> right after the shooting, as she wrote a play that was supposed to have
>>>> been
>>>> debuted at the Holocaust Museum last night. Her play is about Emmett Till,
>>>> whose lynching helped launch the Civil Rights Movement, and Ann Frank,
>>>> whose
>>>> diary told the story of Holocaust victims in hiding in the Netherlands
>>>> during World War II.
>>>>
>>>> She said something must be done about ridding the Internet and the public
>>>> dialogue of hate speech. I agree. Not only have we had three hate crime
>>>> murders within the last two weeks (Mr. Johns, as noted above, Dr. George
>>>> Tiller a week ago last Sunday, and Pvt. William Andrew Long by an
>>>> American-born Muslim convert outside a recruiting station just before
>>>> that.)
>>>>
>>>> Now we have this quote from the so-called Rev. Jeremiah Wright, who used
>>>> to
>>>> be President Obama's pastor. Hate comes from among all peoples and all
>>>> religions. He said this about his lack of communication with Barack Obama
>>>> since he's been elected president, according to the AP:
>>>>
>>>> "Them Jews ain't going to let him talk to me. I told my baby daughter that
>>>> he'll talk to me in five years when he's a lame duck, or in eight years
>>>> when
>>>> he's out of office," Wright told the Daily Press of Newport News following
>>>> a
>>>> Tuesday night sermon at the 95th annual Hampton University Ministers'
>>>> Conference.
>>>>
>>>> It's not enough to prosecute these murders as murders. They are
>>>> hate-motivated crimes and each of these men had been under some sort of
>>>> police surveillance prior to their actions. Isn't it time we started
>>>> rounding up promoters of hate before they kill?
>>>>
>>>> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
>>>>
>>> Make sure kurt knoll is first. We've
>>> already got benji
>> really?
>
> Just ask eugene

Eugenie is in hiding. Or hadn't you noticed. Someone maybe took him
down. I rather enjoyed his discourses. They, unlike yours made some sense.

Heinrich

6/14/2009 10:03:00 AM

0


"Ben Cramer#1" <No1@Cloned.com> schreef in bericht
news:79jvc4F1rmh0dU4@mid.individual.net...
> I'll Always Be 14/06/09 wrote:
>> In article <79js7eF1qt8dhU2@mid.individual.net>,
>> Ben Cramer#1 <No1@Cloned.com> wrote:
>>
>>> I'll Always Be 14/06/09 wrote:
>>>> In article <79jgniF1qpjrfU1@mid.individual.net>,
>>>> "Heinrich" <Heinrich@Ruhrgasnet.de> wrote:
>>>>
>>>>> If yesterday's Holocaust Museum slaying of security guard and national
>>>>> hero Stephen Tyrone Johns is not a clarion call for banning hate
>>>>> speech, I don't know what is. Playwright Janet Langhart Cohen appeared
>>>>> on CNN yesterday right after the shooting, as she wrote a play that
>>>>> was supposed to have been debuted at the Holocaust Museum last night.
>>>>> Her play is about Emmett Till, whose lynching helped launch the Civil
>>>>> Rights Movement, and Ann Frank, whose diary told the story of
>>>>> Holocaust victims in hiding in the Netherlands during World War II.
>>>>>
>>>>> She said something must be done about ridding the Internet and the
>>>>> public dialogue of hate speech. I agree. Not only have we had three
>>>>> hate crime murders within the last two weeks (Mr. Johns, as noted
>>>>> above, Dr. George Tiller a week ago last Sunday, and Pvt. William
>>>>> Andrew Long by an American-born Muslim convert outside a recruiting
>>>>> station just before that.)
>>>>>
>>>>> Now we have this quote from the so-called Rev. Jeremiah Wright, who
>>>>> used to be President Obama's pastor. Hate comes from among all peoples
>>>>> and all religions. He said this about his lack of communication with
>>>>> Barack Obama since he's been elected president, according to the AP:
>>>>>
>>>>> "Them Jews ain't going to let him talk to me. I told my baby daughter
>>>>> that he'll talk to me in five years when he's a lame duck, or in eight
>>>>> years when he's out of office," Wright told the Daily Press of Newport
>>>>> News following a Tuesday night sermon at the 95th annual Hampton
>>>>> University Ministers' Conference.
>>>>>
>>>>> It's not enough to prosecute these murders as murders. They are
>>>>> hate-motivated crimes and each of these men had been under some sort
>>>>> of police surveillance prior to their actions. Isn't it time we
>>>>> started rounding up promoters of hate before they kill?
>>>>>
>>>>> http://www.cbsnews.com/stories/2009/06/12/usnews/whispers/main508...
>>>>>
>>>> Make sure kurt knoll is first. We've already got benji
>>> really?
>>
>> Just ask eugene
>
> Eugenie is in hiding. Or hadn't you noticed. Someone maybe took him down.
> I rather enjoyed his discourses. They, unlike yours made some sense.

that is very nice of you to say, at least one american who seems to
understand what i mean.