0

i simplify my code:

print "hello"

# BeginTime is time in this moment e.g. 9h 45m 23s
# timeplus10s is time 9h 45m 33s

while BeginTime < timeplus10s:
    print "i'm doing something"
print "hello after 10 seconds"

as you can see i wanna create construction in while cycle that will last for 10 seconds.

I can't use time.sleep() because I need to do something (which you can' t see because it's simplified).

How can i do that?

2
  • 1
    That is not valid Python. Commented May 3, 2015 at 16:44
  • ...and now code fixed. Commented May 3, 2015 at 16:53

3 Answers 3

2

It sounds that a task executing during on time frame, try it out:

import datetime as d

def task():
    print "I'm doing something!"

#10 seconds from now
END_TIME = d.datetime.now() + d.timedelta(seconds=10)

while d.datetime.now() < END_TIME:
    task()

print "hello after 10 seconds"
Sign up to request clarification or add additional context in comments.

Comments

1
import time

print "hello"

# BeginTime is time in this moment e.g. 9h 45m 23s
# timeplus10s is time 9h 45m 33s

BeginTime = time.time()
timeplus10s = BeginTime + 10
while BeginTime < timeplus10s:
    BeginTime = time.time()
    print "i'm doing something"

print "hello after 10 seconds"

worked for me. Basically, you only need to save the current time and then check how it changed and if it changed the amount you want, you are done.

Comments

0
from time import time

print "hello"

end_time = time() + 10
while time() < end_time:
    print "i'm doing something"

print "hello after 10 seconds"

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.