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

Discussion in 'Python' started by Sorin Schwimmer, Aug 28, 2006.

  1. I am thinking on something in the following form:

    import time
    import thread


    def fn()
    global delay


    while delay:
    <statement 1>
    <statement 2>

    while <other_condition>:
    <statement 1>
    <statement 2>

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

    while True:
    <statement 1>
    <statement 2>
    if <other_condition>: break

    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.


    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    Sorin Schwimmer, Aug 28, 2006
    1. Advertisements

  2. It doesn't.

    Claudio Grondi, Aug 28, 2006
    1. Advertisements

  3. Sorin Schwimmer

    Steve Holden Guest

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

    who should perhaps have followed his own advice
    Steve Holden, Aug 29, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.