4

I have a program which includes execution of time based while loop. Some thing like..

import time
endtime=time.time()+60.0 #1minute
while (time.time()<endtime):
    do something

I was just wondering if this is possible using for loop? Can I build a time based for loop in python?

6
  • for i in xrange(1000000): if (time.time()<endtime):, It won't be as robust as the while loop but it is a kind of workaround. Commented Apr 30, 2015 at 10:33
  • What loop values do you want the for loop to iterate over? Whatever you want, you can build an iterator for it, but you have to know what you want. Commented Apr 30, 2015 at 10:33
  • @abarnert: I dont want any exact number. I just want it to work for a particular amount of time. Commented Apr 30, 2015 at 10:34
  • Why not just nest your for loop work inside the timed while loop? I still don't understand what your use case is. Commented Apr 30, 2015 at 10:35
  • 2
    @BhoomikaSheth: Then what's wrong with what you already have? The only reason a for loop would be better is if you needed some value each time through the loop. Commented Apr 30, 2015 at 10:37

3 Answers 3

4

Sure. Here's an iterator that gives you the time since start over and over until it reaches the end:

def time_counter(seconds):
    starttime = time.time()
    while True:
        now = time.time()
        if now > starttime + seconds:
            break
        yield now - starttime

for t in time_counter(20):
    print(t)
    time.sleep(3)

When I run this, it outputs:

9.5367431640625e-07
3.002220869064331
6.0040669441223145
9.004395961761475
12.006848812103271
15.009617805480957
18.011652946472168

If you need some different value, just change the yield expression.

If you don't need any value… then you don't need a for statement; your existing code is already perfectly readable, and a for loop that iterates over and discards some meaningless values is just going to make it confusing.

Sign up to request clarification or add additional context in comments.

Comments

3

From https://wiki.python.org/moin/ForLoop

When do I use for loops? For loops are traditionally used when you have a piece of code which you want to repeat n number of times. As an alternative, there is the WhileLoop, however, while is used when a condition is to be met, or if you want a piece of code to repeat forever, for example -

The while loop feels more natural for this task, because the for needs an enumerable or a generator.

But if you really want to do it with a for loop, I guess you can construct a generator that yields something until time.time()<endtime:

def there_is_more_time(e)
    while time.time() < e:
        yield True

for i in there_is_more_time(endtime):
    do something

But as you see, you are again using while behind the scenes.

Perhaps someone can manage to not use the while inside the generator, but what's the point of doing so?

1 Comment

Sure, you could avoid the inner while with, say, for i in repeat(None):. Which hopefully demonstrates why you're right, that there is no point in doing so…
1

Perhaps you want to create a scheduler object?

https://docs.python.org/2/library/sched.html

import sched, time
s = sched.scheduler(time.time, time.sleep)

def print_time(): 
    print "From print_time", time.time()

def print_some_times():
    print time.time()
    s.enter(1, 1, print_time, ())
    s.enter(2, 1, print_time, ())
    s.enter(3, 1, print_time, ())
    s.enter(4, 1, print_time, ())
    s.enter(5, 1, print_time, ())
    s.run()
    print time.time()

Output:

1430392956.35
From print_time 1430392957.35
From print_time 1430392958.35
From print_time 1430392959.35
From print_time 1430392960.35
From print_time 1430392961.35
1430392961.35

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.