Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to let a loop run for a while before checking for break condition?

Reply
Thread Tools

How to let a loop run for a while before checking for break condition?

 
 
Sorin Schwimmer
Guest
Posts: n/a
 
      08-28-2006
I am thinking on something in the following form:

<code>
import time
import thread

delay=True

def fn()
global delay
time.sleep(<your_amount_of_time_in_seconds>)
delay=False

thread.start_new_thread(fn,())

while delay:
<statement 1>
<statement 2>
...

while <other_condition>:
<statement 1>
<statement 2>
...
</code>

Or, if you need to use "break", the second loop may be
something like:

<code>
while True:
<statement 1>
<statement 2>
...
if <other_condition>: break
<other_statements>
...
</code>

The two while loops have the same content, but the
first is terminated after a pre-determined amount of
time, while the second by another condition. Usually
the content of the too loops, being the same, is a
call to a function that does the actual work. In your
case, as time seems to be critical, you don't want to
spend it in function-call overheads, so you repeat
(cut'n'paste) the relevant code.

Of course, the price to be paid is in maintenance
headache: you'll have to make all the changes in both
loops, to keep consistency.

I hope this helps.

Sorin

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 
Reply With Quote
 
 
 
 
Claudio Grondi
Guest
Posts: n/a
 
      08-28-2006
Sorin Schwimmer wrote:
> I am thinking on something in the following form:
>
> <code>
> import time
> import thread
>
> delay=True
>
> def fn()
> global delay
> time.sleep(<your_amount_of_time_in_seconds>)
> delay=False
>
> thread.start_new_thread(fn,())
>
> while delay:
> <statement 1>
> <statement 2>
> ...
>
> while <other_condition>:
> <statement 1>
> <statement 2>
> ...
> </code>
>
> Or, if you need to use "break", the second loop may be
> something like:
>
> <code>
> while True:
> <statement 1>
> <statement 2>
> ...
> if <other_condition>: break
> <other_statements>
> ...
> </code>
>
> The two while loops have the same content, but the
> first is terminated after a pre-determined amount of
> time, while the second by another condition. Usually
> the content of the too loops, being the same, is a
> call to a function that does the actual work. In your
> case, as time seems to be critical, you don't want to
> spend it in function-call overheads, so you repeat
> (cut'n'paste) the relevant code.
>
> Of course, the price to be paid is in maintenance
> headache: you'll have to make all the changes in both
> loops, to keep consistency.
>
> I hope this helps.

It doesn't.

Claudio
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      08-29-2006
Claudio Grondi wrote:
> Sorin Schwimmer wrote:

[...]
> It doesn't.
>
> Claudio


Sometimes silence is preferable to a concrete response. It takes less
time and occupies less bandwidth.

regards
Steve
who should perhaps have followed his own advice
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
How to let a loop run for a while before checking for break condition? Claudio Grondi Python 16 08-30-2006 05:40 AM
How to let a loop run for a while before checking for break condition? Sorin Schwimmer Python 1 08-28-2006 06:03 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments